Using Entity Framework navigation properties witho

2019-02-19 05:48发布

问题:

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.