How do I fill the entity's navigation attributes using a stored procedure in Entity Framework?

.net c# entity entity-framework


I tried utilizing a stored method since Entity Framework is so painfully sluggish, but I came across this issue.

You may provide a stored procedure that generates an entity using Entity Framework. However, while utilizing this approach, the "navigation properties" of my object are not being filled in.

Is there an alternative?

4/3/2018 10:38:07 AM

Accepted Answer

Well-maintained procedures cannot be compiled. There is thus no way to call your SPROC and have the EF use Include() or whatever to automatically fill relationships in the same query.

Let's imagine you have categories and goods.

you have a sproc to get goods:


var products = context.GetProducts(someproductfilter);

the categories won't be loaded in the resultant goods.

On the other hand, if you have a second stored process that retrieves the Categories for those products:


var categories = context.GetCategoriesForProducts(someproductfilter);

After both calls are done, each product in products will have a non-null Category thanks to a feature in EF called connection fixup, which joins related entities after the second entity enters the context.

Because you are running many queries, this is not ideal, but it will work.

You might also use EFExtensions. The author of that piece made it possible to construct sprocs that load more data all at once.

Hope this is useful.

Cheers Alex

6/24/2009 12:13:14 AM

Popular Answer

While using EF to explore Stored Procedures (SPs), I came across this SO query. I also saw inquiries about an EF6 upgrade from @KristianNissen and @Todilo, among others.

Yes, EF 6 has made changes, however nothing to aid load navigational properties while utilizing SPs was included. You also cannot utilize the. As requested in this Why query, use the Include() method with SPs.

The only option is to explicitly load the navigational attributes in your SP. However, there is currently some excellent Microsoft guidance available on utilizing SPs.

For completeness, EF version 6 changed the way insert, update, and delete operations may be handled by Stored Procedures (SPs); for further information, see Microsoft document and Entire Entity Framework.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow