c# - EF6 - code-first - how to properly handle deleting a parent entry from an m:n relationship -


i have situation have 2 entities - itemgroup , item - linked in m:n fashion:

create table itemgroup (     itemgroupid int identity(1,1) primary key clustered,     itemgroupname varchar(50) )  create table item (     itemid int identity(1,1) primary key clustered,     itemname varchar(50) )  create table itemgroup_item (     itemgroupid int not null         constraint fk_itemgroupitem_itemgroup         foreign key references dbo.itemgroup(itemgroupid),     itemid int not null         constraint fk_itemgroupitem_item         foreign key references dbo.item(itemid),      constraint pk_itemgroup_item         primary key clustered(itemgroupid, itemid) ) 

therefore, in sql server database, have "link" table connects 2 entities including respective primary keys.

when re-engineer ef 6 model, these 2 classes:

[table("item")] public partial class item {     public item()     {         itemgroup = new hashset<itemgroup>();     }      public int itemid { get; set; }      [stringlength(50)]     public string itemname { get; set; }      public virtual icollection<itemgroup> itemgroup { get; set; } }  [table("itemgroup")] public partial class itemgroup {     public itemgroup()     {         item = new hashset<item>();     }      public int itemgroupid { get; set; }      [stringlength(50)]     public string itemgroupname { get; set; }      public virtual icollection<item> item { get; set; } } 

and dbcontext class contains fluent setup:

public partial class itemgroupmodel : dbcontext {     public itemgroupmodel() : base("name=itemgroupconn")     { }      public virtual dbset<item> item { get; set; }     public virtual dbset<itemgroup> itemgroup { get; set; }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         modelbuilder.entity<item>()             .property(e => e.itemname)             .isunicode(false);          modelbuilder.entity<item>()             .hasmany(e => e.itemgroup)             .withmany(e => e.item)             .map(m => m.totable("itemgroup_item").mapleftkey("itemid").maprightkey("itemgroupid"));          modelbuilder.entity<itemgroup>()             .property(e => e.itemgroupname)             .isunicode(false);     } } 

my troubles start when i'm trying delete itemgroup:

using(itemgroupmodel ctx = new itemgroupmodel()) {     itemgroup grp = ctx.itemgroup.firstordefault();      ctx.itemgroup.remove(grp);     ctx.savechanges(); } 

what want have ef in case delete entries "link" table (which isn't modelled separate entity in ef), , delete entry in itemgroup itself. entries in item must not touched!

trouble : since link table isn't actual entity in ef model, how can ensure link entries in fact deleted, not item link to? cannot handle deleting "child" entries manually - there no child entries can access....

how can handle nicely? seems work - other times exception saying delete statement conflicted entries still present in itemgroup_item ....

if stumbles across problem, - here's solution: needed call .clear() method on navigation property of itemgroup object before saving. "disconnects" item's itemgroup linked to, without removing underlying item objects - entries in junction table gone:

using(itemgroupmodel ctx = new itemgroupmodel()) {     itemgroup grp = ctx.itemgroup.firstordefault();      // call item.clear() remove entries in navigation     // property translates entries in junction table     // in underlying database     grp.item.clear();      ctx.itemgroup.remove(grp);     ctx.savechanges(); } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

unity3d - Fatal error- Monodevelop-Unity failed to start -