简体   繁体   English

更新和编辑多对多表Entity Framework

[英]Update and edit many to many tables Entity Framework

I have four tables (Locations, Events, MainTags, EventTags) and between the Events table and each one of the other three tables is a many-to-many relationship. 我有四个表(Locations,Events,MainTags,EventTags), Events表与其他三个表中的每个表之间是多对多关系。

Now I want to edit and display data from those tables on the same page but I'm not sure what is the right way to do it. 现在,我想在同一页面上编辑和显示这些表中的数据,但是我不确定哪种方法是正确的。

For now I'm doing it like this taking the id from each individual entry in the Events table and then searching for the same id in each of the other table, saving those results in a list and later displaying it on the page. 现在,我这样做是从“ Events表中的每个单独条目中获取ID,然后在每个其他表中搜索相同的ID,然后将这些结果保存在列表中,然后将其显示在页面上。 ( adc is my connection to the database) adc是我与数据库的连接)

List<Event> allEvents = adc.Events.ToList();
List<Location> testTHis = new List<Location>();

foreach (Event eve1 in allEvents)
{
   var query_test = from a in adc.Locations where a.Id ==  eve1.id select a;
   testTHis = query_test.ToList();
}

But this seems to be rather an intensive work and it doesn't look nice so I'm wondering if there is a much better way to do this especially later when a database has a lot of data in it this doesn't seem like a good idea cause it will take a lot of time to complete. 但这似乎是一项繁重的工作,而且看起来不太好,所以我想知道是否有更好的方法可以做到这一点,尤其是在数据库中包含大量数据的情况下,这看起来似乎不可行。好主意,因为它需要很多时间才能完成。 Anyone can help me to figure out a better solution? 任何人都可以帮助我找出更好的解决方案吗?

Your Event entity should expose 3 navigational properties: 您的Event实体应公开3个导航属性:

public virtual ICollection<Location> Locations { get; set; }
public virtual ICollection<MainTag> MainTags { get; set; }
public virtual ICollection<EventTag> EventTags { get; set; }

Then when you query for events you tell EF to include all tags/event tags and locations. 然后,当您查询事件时,您告诉EF包括所有标签/事件标签和位置。

context.Events
       .Include(x => x.Locations)
       .Include(x => x.MainTags)
       .Include(x => x.EventTags)
       .ToList()

This will retrieve events as well as their Locations, MainTags and EventTags all together in one query. 这将在一个查询中一起检索事件及其位置,MainTag和EventTag。

One word of warning with include is, each event will have its own Tag/Location, even if its referenced multiple times. 带有警告的警告是,即使多次引用,每个事件也将具有其自己的标记/位置。

Eg lets say you have 2 Events, 例如,假设您有2个活动,

  1. CodePerfDotNetEvent { Tag={ID=1, Name="Blog"} } CodePerfDotNetEvent {Tag = {ID = 1,Name =“ Blog”}}
  2. MichalCiechanEvent { Tag={ID=1, Name="Blog"} } MichalCiechanEvent {Tag = {ID = 1,Name =“ Blog”}}

When you use include you will get 2 different Tag objects even though they represent the same value in the database. 当使用include时,即使它们在数据库中表示相同的值,也将获得2个不同的Tag对象。

Otherwise you leave LazyLoading and ProxyGeneration on, and let EF make multiple calls to the database each time you access one of the Locations/EventTags/MainTags virtual properties. 否则,您将LazyLoading和ProxyGeneration保持打开状态,并让EF每次访问Locations / EventTags / MainTags虚拟属性之一时对数据库进行多次调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM