Entity Framework 6.12 cascade Delete ( one to many relationship)

依然范特西╮ 提交于 2019-12-24 16:15:57

问题


i have two table parent(id p_key,name) and child(addresid,city, id ForeignKey) table have one to many relationship ,

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


回答1:


Add this to your DB 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



来源:https://stackoverflow.com/questions/33210831/entity-framework-6-12-cascade-delete-one-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!