简体   繁体   English

如何使用Entity Framework显式加载映射表?

[英]How to explicitly load mapping tables with Entity Framework?

I have to optimize my Entity Framework queries as the generated SQL is getting more and more complex because of multiple .Include(...) statements. 由于多个.Include(...)语句,生成的SQL变得越来越复杂,因此我必须优化Entity Framework查询。

Instead of using Includes, I started to use explicit loading which is nice, since I have some small tables which are better to be loaded explicitly by the Load method of QueryableExtensions. 我不再使用Includes,而是开始使用显式加载,这很好,因为我有一些较小的表,最好通过QueryableExtensions的Load方法显式加载

However, I cannot find an easy way to explicitly load my mapping tables for many-to-many connected tables - since the mapping table is not mapped to a DbSet... 但是,我找不到为多对多连接表显式加载映射表的简便方法-因为映射表未映射到DbSet ...

I use Code First with mappings like this: 我将“代码优先”与这样的映射一起使用:

 modelBuilder.Entity<SomeEntity>()
             .HasMany(a => a.SomeProperty)
             .WithMany()
             .Map(c =>
             {
                  c.ToTable("MappingTable");
             });

Is there any easy way to explicity load the MappingTable without mapping them? 有什么简单的方法可以显式地加载MappingTable而不映射它们?

Now I realized Include statements generate UNIONs when I include multiple (at least 2) fields with many-to-many relationships - it seems to be logical, although at first I didn't see what was going on in the background. 现在,我意识到当我包含具有多对多关系的多个(至少2个)字段时, Include语句会生成UNION-这似乎是合乎逻辑的,尽管起初我不知道后台发生了什么。 The problem with multiple UNIONs is that the query could be quite complex if there are a lot of them. 多个UNION的问题在于,如果有很多UNION,查询可能会非常复杂。

It turned out that I can "prefetch" the many-to-many connections using the same query filter. 原来,我可以使用相同的查询过滤器“预取”多对多连接。 If I do the prefetching for every many-to-many connection, this simplifies the query (although it makes multiple roundtrips to the DB server, but it's not a big issue for me). 如果我为每个多对多连接进行预取,这将简化查询(尽管它使数据库服务器进行了多次往返,但这对我来说不是一个大问题)。

So to illustrate it with an example - instead of writing something like: 因此,用一个例子来说明它-而不是像这样写:

var query = ctx.Employee
               .Include(e => e.ContactAddresses)
               .Include(e => e.Contracts)
               .MyFilter();

query.ToList().Select(e => 
{
    ContactNames = e.ContactAddresses.Select(c => c.DisplayName),
    ContractNames = e.Contracts.Select(c => c.DisplayName)
});

Write something like: 写类似:

ctx.Employee.Include(e => e.ContactAddresses).MyFilter().ToList();
ctx.Employee.Include(e => e.Contracts).MyFilter().ToList();


var query = ctx.Employee
               .MyFilter();

query.ToList().Select(e => 
{
    ContactNames = e.ContactAddresses.Select(c => c.DisplayName),
    ContractNames = e.Contracts.Select(c => c.DisplayName)
});

Where MyFilter() contains your filtering logic which are part of the queries (Where statements, etc...) MyFilter()包含查询的一部分的过滤逻辑(Where语句等)。

And this solution is also superior to explicitly loading the whole mapping tables (which was my first idea - see the original question), because the mapping tables can be quite big in certain situations. 而且,此解决方案也优于显式加载整个映射表(这是我的第一个想法-参见原始问题),因为在某些情况下映射表可能很大。

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

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