2013-07-01 35 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