Questions about how to update and insert a Many-to-many relational table _ Practical Tips

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.