【问题标题】:Code First Configuration Error代码优先配置错误
【发布时间】:2013-06-28 17:24:31
【问题描述】:

出于学习目的,我正在尝试先使用代码对简单的 QA 应用程序进行建模。用户应该能够提出问题、回答问题并为问题和答案编写 cmets。这是我的模型类:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    public string UserName { get; set; }

    public DateTime? BirthDate { get; set; }

    public ICollection<Gym> Gyms { get; set; }
}

[Table("Question")]
public class Question
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int QuestionId { get; set; }

    public int UserProfileId { get; set; }

    [ForeignKey("UserProfileId")]
    public UserProfile UserProfile { get; set; }

    public string Header { get; set; }

    public string Content { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime CreateDate { get; set; }

    public ICollection<Answer> Answers { get; set; }

    public ICollection<QuestionComment> QuestionComments { get; set; }
}

[Table("QuestionComment")]
public class QuestionComment
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int QuestionCommentId { get; set; }

    public int UserProfileId { get; set; }

    [ForeignKey("UserProfileId")]
    public UserProfile UserProfile { get; set; }

    public int QuestionId { get; set; }

    [ForeignKey("QuestionId")]
    public Question Question { get; set; }

    [Column("Content", TypeName = "ntext")]
    public string Content { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime CreateDate { get; set; }
}

[Table("Answer")]
public class Answer
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int AnswerId { get; set; }

    public int UserProfileId { get; set; }

    [ForeignKey("UserProfileId")]
    public UserProfile UserProfile { get; set; }

    public int QuestionId { get; set; }

    [ForeignKey("QuestionId")]
    public Question Question { get; set; }

    [Column("Content", TypeName="ntext")]
    public string Content { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime CreateDate { get; set; }

    public IList<AnswerComment> AnswerComments { get; set; }
}

[Table("AnswerComment")]
public class AnswerComment
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int AnswerCommentId { get; set; }

    public int UserProfileId { get; set; }

    [ForeignKey("UserProfileId")]
    public UserProfile UserProfile { get; set; }

    public int AnswerId { get; set; }

    [ForeignKey("AnswerId")]
    public Answer Answer { get; set; }

    [Column("Content", TypeName = "ntext")]
    public string Content { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime CreateDate { get; set; }
}

这是我的数据库上下文类:

public class TestDbContext : DbContext
{
    public TestDbContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Question> Questions { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // After update
        modelBuilder.Entity<UserProfile>()
            .HasMany(p => p.Questions)
            .WithRequired()
            .HasForeignKey(c => c.UserProfileId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<UserProfile>()
            .HasMany(p => p.Answers)
            .WithRequired()
            .HasForeignKey(c => c.UserProfileId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<UserProfile>()
            .HasMany(p => p.AnswerComments)
            .WithRequired()
            .HasForeignKey(c => c.UserProfileId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<UserProfile>()
            .HasMany(p => p.QuestionComments)
            .WithRequired()
            .HasForeignKey(c => c.UserProfileId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Question>()
            .HasMany(p => p.Answers)
            .WithRequired()
            .HasForeignKey(c => c.QuestionId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Question>()
            .HasMany(p => p.QuestionComments)
            .WithRequired()
            .HasForeignKey(c => c.QuestionId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Answer>()
            .HasMany(p => p.AnswerComments)
            .WithRequired()
            .HasForeignKey(c => c.AnswerId)
            .WillCascadeOnDelete(false);
        // After update
    }
}

使用上述声明创建数据库时出现以下错误:

在表“AnswerComment”上引入 FOREIGN KEY 约束“AnswerComment_UserProfile”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。\r\n无法创建约束。查看以前的错误。

我该怎么做才能解决这个问题?

提前致谢,

【问题讨论】:

    标签: c# asp.net entity-framework ef-code-first


    【解决方案1】:

    您有一个表 A,其中包含另一个表 B 的 FK。 同时,表 A 包含表 C 的 FK。 现在,表 B 和 C 都包含表 D 的 FK。

    如果所有 FK 都定义为删除级联,则表 C 中的记录可以被删除两次。 这不是逻辑问题,但 SQL Server 不支持此选项。

    为避免此问题设置为删除无操作。

    【讨论】:

    • 我怎样才能为这种情况设置删除不操作?
    • 在您的 FK 中,尝试添加 cascadeDelete=false
    【解决方案2】:

    尝试将此添加到您的 UserProfile 实体中:

    public virtual ICollection<AnswerComment> AnswerComments { get; set; }
    

    然后将此添加到您的modelBuilder,这应该可以消除您问题中的错误,您可能必须为QuestionCommentQuestionAnswer 执行此操作:

    modelBuilder.Entity<AnswerComment>()
                .HasRequired(u => u.UserProfile)
                .WithMany(a => a.AnswerComments)
                .WillCascadeOnDelete(false);
    

    【讨论】:

    • 你确定吗?添加这些行后,我无法构建解决方案。我得到:无法将类型“System.Collections.Generic.ICollection”隐式转换为“NS.Model.Question.AnswerComment”。存在显式转换(您是否缺少演员表?)
    • @anilca 播种我的编辑,尝试将 WithOptional 更改为 WithMany
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多