我正在嘗試使用代碼首先爲學習目的建立一個簡單的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不能創建約束。查看以前的錯誤。
我需要做些什麼來解決這個問題?
由於提前,
你確定嗎?添加這些行後,我無法構建解決方案。我得到:不能隱式地將類型'System.Collections.Generic.ICollection'轉換爲'NS.Model.Question.AnswerComment'。存在明確的轉換(您是否缺少演員?) –
anilca
@anilca種子我的編輯,嘗試將'WithOptional'更改爲'WithMany'。 – SOfanatic
是的,它現在可以正常工作。非常感謝。 – anilca