In the EF, when we design the model, will be designed to a many-to-many relationship, in the EF will turn this relationship into two one-to-many relational table, which is more friendly, because many to many, for the business itself is not interesting, so hidden, no harm, but for this hidden, For developers you will not be able to actively control this relationship table, and the need to use the EF update of the main table to upgrade the same time to update the relational table, which has some problems for beginners, today is a many-to-many relationship can not be updated and inserted problems.
Data
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
Reference: Https://msdn.microsoft.com/en-us/data/jj556205.aspx
Uncle's explanation, when autodetectchangesenabled is true, you can load dependent relationships, when inserts and updates are synchronized (Many-to-many, one-to-many relationships are used), and when the value is False, update only (insert) the data for the primary table
Problem solving
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, a one-to-many curd operation needs to be true this
. configuration.lazyloadingenabled = false;
This. configuration.proxycreationenabled = false;//prohibit dynamic interception of System.Data.Entity.DynamicProxies.
}
The above is a small set for you to introduce a many-to-many relationship table can not update and insert the problem, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!