更新时间:2023-02-14 20:07:20
您需要在DBContext
这可能是一对多,一对一,多对多
您可以在此处
我使用的是MVC的默认ApplicationDbContext,因此数据库中有ApplicationUser的表.
I'm using MVC's default ApplicationDbContext and therefore have ApplicationUser's tables in my database.
如果要继承ApplicationDbContext
,则应在模型创建方法中调用base.OnModelCreating()
方法.
if you are inheriting the ApplicationDbContext
you should call the base.OnModelCreating()
method in your model creating method.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PostReply>()
.HasRequired(p => p.Post)
.WithMany(p => p.Replies)
.HasForeignKey(r => r.PostId)
.WillCascadeOnDelete(true);
}
要启用级联删除,您应该发送.WillCascadeOnDelete(true)
And to enable cascade delete, you should send true parameter like .WillCascadeOnDelete(true)