Home>

In ef, when we design the model,Will design many-to-many relationships,In EF, this relationship will be converted into two one-to-many relationship tables.This is more friendly,Because many-to-many,Not interesting for the business itself,So hidden,No harm,But for this hiding,For developers, you can't actively control this relationship table,You need to use EF update to update the main table while updating the relational table.This can cause some problems for beginners,Today is talking about the problem that many-to-many relationships cannot be updated and inserted.

data structure

public partial class webmanageroles:lind.ddd.domain.entity
{
public webmanageroles ()
{
this.webmanagemenus=new list<webmanagemenus>();
this.webmanageusers=new list<webmanageusers>();
}
[displayname ("name"), required]
public string rolename {get;set;}
[displayname ("About")]
public string about {get;set;}
[displayname ("Sort"), required]
public int sortnumber {get;set;}
[displayname ("last operator")]
public string operator {get;set;}
[displayname ("permission"), required]
public int operatorauthority {get;set;}
[displayname ("department"), required]
public int departmentid {get;set;}
public virtual webdepartments webdepartments {get;set;}
public virtual icollection<webmanagemenus>webmanagemenus {get;set;}
public virtual icollection<webmanageusers>webmanageusers {get;set;}
}

About autodetectchangesenabled

Uncle's explanation,When autodetectchangesenabled is true, dependencies can be loaded,Insertion and update will be completed synchronously (used in many-to-many, one-to-many relationships). When the value is false, only the data of the main table is updated (inserted)

problem solved

old.webmanagemenus=menurepository.getmodel (i =>menu.contains (i.id)). tolist ();
old.departmentid=dept;
old.rolename=entity.rolename;
old.sortnumber=entity.sortnumber;
old.about=entity.about;
old.dataupdatedatetime=datetime.now;
rolerepository.update (old);

Settings in the data context

public managercontext ()
:base ("defaultconnection")
{
this.configuration.autodetectchangesenabled=true;//to many to many,Need to be true for one-to-many curd operations
this.configuration.lazyloadingenabled=false;
this.configuration.proxycreationenabled=false;//Disable dynamic interception of system.data.entity.dynamicproxies.
}