Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that many-to-many relational tables cannot be updated and inserted

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to solve the problem that many-to-many tables can not be updated and inserted, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

In EF, when we design a model, we design a many-to-many relationship, and in EF, we turn this relationship into two one-to-many relational tables, which is friendly, because many-to-many is not meaningful to the business itself, so it doesn't do any harm to hide it, but for this hiding, you can't actively control the relationship table for the developer. While you need to use EF's update to update the main table and update the relational table at the same time, there will be some problems for beginners. Today we are 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 (); this.WebManageUsers = new List ();} [DisplayName ("name"), Required] public string RoleName {get; set;} [DisplayName (about ")] public string About {get; set;} [DisplayName (" sort "), Required] public int SortNumber {get; set;} [DisplayName (" final 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 {get; set;} public virtual ICollection WebManageUsers {get; set;}}

About AutoDetectChangesEnabled

Reference: https://msdn.microsoft.com/en-us/data/jj556205.aspx

Uncle's explanation is that when AutoDetectChangesEnabled is true, dependent relationships can be loaded, inserts and updates will be completed synchronously (many-to-many, one-to-many relationships), and when the value is false, only the data of the main table will be updated (inserted).

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;// many-to-many, one-to-many curd operations need to disable dynamic interception of System.Data.Entity.DynamicProxies for truethis.Configuration.LazyLoadingEnabled = false;this.Configuration.ProxyCreationEnabled = false;//.} is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 258

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report