Sunday, 15 March 2015

c# - Entity Framework saveChanges does not work with some entities -



c# - Entity Framework saveChanges does not work with some entities -

i have db structure:

user company companyuser

companyuser has userid , companyid column , boolean.

user entity has navigation item:

icollection<companyuser> companies

company has navigation item:

icollection<companyuser> users

companyuser looks this:

public class companyuser : baseentity { /// <summary> /// gets or sets if user admin /// </summary> public bool isadmin { get; set; } /// <summary> /// gets or sets company id /// </summary> public guid? companyid { get; set; } /// <summary> /// gets or set user id /// </summary> public guid? userid { get; set; } public user user { get; set; } public company company { get; set; } }

i can create user , company entities , insert database. when create companyuser not insert @ all.

user user = new user(); // set properties _userservice.insertuser(user); company company = _companyservice.getcompanybyid(i); companyuser companyuser = new companyuser() { isadmin = true, companyid = company.id, userid = user.id }; _companyservice.insertcompanyuser(companyuser);

i cannot understand why not insert. have debugged , there no exceptions beingness thrown or caught don't know about. when view companyuser contains looks perfect.

savechanges() returns 0 every companyuser alter , entities' count 0

this entity framework has been throwing me around past few hours , can't seem figure 1 out. doing wrong?

update

here insertcompanyuser

try { if (entity == null) throw new argumentnullexception("entity"); this.entities.add(entity); this._context.savechanges(); }

this.entities idbset<t>

my db context extends dbcontext , has method:

public new idbset<tentity> set<tentity>() tentity : baseentity { homecoming base.set<tentity>(); }

update 2

the dbset companyuser retains count of 0 , never increases company , user dbsets increasing should. why dbset not increment when calling add

check object model generating tables correctly related using primary / foreign keys, seek approach:

user user = new user(); // set properties company company = _context.companies.find(i); companyuser companyuser = new companyuser() { isadmin = true, user = user }; company.users.add(companyuser); _context.savechanges();

c# asp.net-mvc entity-framework

No comments:

Post a Comment