Entity Framework 6.12级联删除(一对多关系)
Entity Framework 6.12 cascade Delete ( one to many relationship)
我有两个 table parent(id p_key,name)
和 child(addresid,city, id ForeignKey)
table 有一对多的关系,
因此,如果我要从父 table 中删除任何记录,则应从子 table 中删除所有相关记录
我正在使用entity framework代码优先方法
将此添加到您的数据库Context
:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<parent>()
.HasOptional(c => c.child)
.WithOptionalDependent()
.WillCascadeOnDelete(true);
}
我有两个 table parent(id p_key,name)
和 child(addresid,city, id ForeignKey)
table 有一对多的关系,
因此,如果我要从父 table 中删除任何记录,则应从子 table 中删除所有相关记录
我正在使用entity framework代码优先方法
将此添加到您的数据库Context
:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<parent>()
.HasOptional(c => c.child)
.WithOptionalDependent()
.WillCascadeOnDelete(true);
}