Using Entity Framework navigation properties without creating lots of queries (avoiding N+1)

╄→尐↘猪︶ㄣ 提交于 2019-12-19 10:18:41

问题


I've been using Entity Framework Profiler to test my data access in an MVC project and have come accross several pages where I'm making far more db queries than I need to because of N+1 problems.

Here is a simple example to show my problem:

var club = this.ActiveClub; // ActiveClub uses code similar to context.Clubs.First() 
var members = club.Members.ToList();
return View("MembersWithAddress", members);

The view loops through Members and then follows a navigion property on each member to also show their address. Each of the address requests results in an extra db query.

One way to solve this would be to use Include to make sure the extra tables I need are queried up front. However, I only seem to be able to do this on the ObjectSet of Clubs attached directly to the context. In this case the ActiveClub property is shared by lots of controllers and I don't always want to query the Member and address table up front.

I'd like to be able to use something like:

var members = club.Members.Include("Address").ToList();

But, Members is an EntityCollection and that doesn't have the Include method on it.

Is there a way to force a load on the Members EntityCollection and ask EF to also load their Addresses?

Or, is using EntityCollection navigation properties on an entity in this way, just a really bad idea; and you should know what you're loading when you get it from the context?


回答1:


If your entities inherits from EntityObject try to use this:

var members = club.Members.CreateSourceQuery()
                          .Include("Address")
                          .ToList();

If you use POCOs with lazy loading proxies try to use this:

var members = ((EntityCollection<Club>)club.Members).CreateSourceQuery()
                                                    .Include("Address")
                                                    .ToList();

Obviously second version is not very nice because POCOs are used to remove dependency on EF but now you need to convert the collection to EF class. Another problem is that the query will be executed twice. Lazy loading will trigger for Members once to access the property and then second query will be executed when you call ToList. This can be solved by turning off lazy loading prior to running the query.

When you say ActiveClub is shared I believe it means something like it is property in base controller used in derived controllers. In such case you can still use different code in different controller to fill the property.



来源:https://stackoverflow.com/questions/5998218/using-entity-framework-navigation-properties-without-creating-lots-of-queries-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!