2013-07-01 34 views
3

我正在尝试使用代码首先为学习目的建立一个简单的QA应用程序。用户应该能够提问,回答问题并为问题和答案写评论。这里是我的模型类:代码优先配置错误

[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不能创建约束。查看以前的错误。

我需要做些什么来解决这个问题?

由于提前,

回答

2

尝试添加以下内容到UserProfile实体:

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

然后添加到您的modelBuilder,这应该让你的问题消除掉的错误,你可能会为QuestionComment做到这一点,Question ,并且Answer

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

你确定吗?添加这些行后,我无法构建解决方案。我得到:不能隐式地将类型'System.Collections.Generic.ICollection '转换为'NS.Model.Question.AnswerComment'。存在明确的转换(您是否缺少演员?) – anilca

+0

@anilca种子我的编辑,尝试将'WithOptional'更改为'WithMany'。 – SOfanatic

+0

是的,它现在可以正常工作。非常感谢。 – anilca

2

您有一个包含FK到另一个表B. 的同时表A中的表A包含FK表C. 现在,这两个表B和C包含FKS到表D.

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

为了避免这个问题,设置删除无操作。

+0

如何设置删除此操作无任何操作? – anilca

+0

在您的FK中,尝试添加cascadeDelete = false – asafrob

+0

我更新了OnModelCreating方法,但它没有帮助。 – anilca