c# - Many to Many is creating new record everytime, code first, EF -
c# - Many to Many is creating new record everytime, code first, EF -
this issue driving me crazy. know has been asked before , have tried solutions. know doing wrong unable find where.
i have next model:
public class user : userentitybase { /// <summary> /// gets or sets roles. /// </summary> public icollection<securityrole> roles { get; set; } }
i have model roles:
public class securityrole : entitybase { public string name { get; set; } public string description { get; set; } public icollection<user> users { get; set; } }
the mappings set as:
hasmany(u => u.roles).withmany(r=>r.users).map(m=>m.totable("securityuserroles").mapleftkey("userid").maprightkey("roleid"));
i want insert new user security role called "everyone". using domain service > repository model.
in user domain service insert method have next code:
var everyonerole = _roleservice.get(rol => rol.name.equals(systemroletypes.everyone.tostring())).singleordefault(); if (everyonerole != null) { entity.roles = new list<securityrole>(); entity.roles.add(everyonerole); } base.insert(entity, out message);
by running insert, inserting record in:
user , securityrole table. create relation in securityuserroles.
i understand why doing due "detach behaviour". however, getting role object database , straight adding system.
please help.
c# .net entity-framework ef-code-first
Comments
Post a Comment