Repopulate Entity Framework Navigation properties
Hi I have an MVC app I'm calling a stored procedure from. The reason I'm using a stored procedure is because the query is quite complex and it already exists so I may as well use it.
It basically returns to me a :
IEnumerable<Activity>
which is good.
There is a one of many foreign key properties like:
AreaId
on this which gets populated.
However in my model I have the navigation property:
// Navigation properties
public virtual Area Area { get; set; }
which of course isn't populated getting it via the stored procedure.
I'm wondering if there is an easy way to get these navigation properties populated.
I be开发者_如何学JAVAlieve I have heard of some command you can call on you entity to refresh navigation properties.
You can use this:
context.Entry(loadedEntity).Reference(l => l.Area).Load();
but it will not have nice performance if you call it for every entity in your enumeration because it will do separate query and database roundtrip for every Area
you want to load. That is disadvantage of using stored procedures - once you go this way you should have another stored procedure to load all needed areas.
精彩评论