gpt4 book ai didi

c# - 代码优先配置错误

转载 作者:行者123 更新时间:2023-11-30 15:33:33 26 4
gpt4 key购买 nike

出于学习目的,我正在尝试先使用代码为简单的 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
}
}

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

Introducing FOREIGN KEY constraint 'AnswerComment_UserProfile' on table 'AnswerComment' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.\r\nCould not create constraint. See previous errors.

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

提前致谢

最佳答案

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

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

然后将其添加到您的modelBuilder,这应该会消除您问题中的错误,并且您可能必须为QuestionComment 执行此操作,问题答案:

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

关于c# - 代码优先配置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17407032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com