[英]Entity Framework 6.12 cascade Delete ( one to many relationship)
i have two table parent(id p_key,name)
and child(addresid,city, id ForeignKey)
table have one to many relationship , 我有两个表parent(id p_key,name)
和child(addresid,city, id ForeignKey)
表具有一对多关系,
so if i am deleting any recording from parent table then all related record should be deleted from child table 因此,如果我要从父表中删除任何记录,那么所有相关记录都应从子表中删除
i am using entity framework code first approach 我正在使用实体框架代码优先方法
Add this to your DB Context
: 将此添加到您的数据库Context
:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<parent>()
.HasOptional(c => c.child)
.WithOptionalDependent()
.WillCascadeOnDelete(true);
}
Have a look at this: Enabling Cascade Delete
看一下: Enabling Cascade Delete
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.