Entity framework is cripplingly slow so I tried using a stored procedure but I ran into this problem.
Entity Framework allows you to define a stored procedure that produces an entity. However my entity has 'navigation properties' which are not being populated when using this method.
Is there a work around?
The Entity Framework has the capability of importing a Stored Procedure as a function. We can also map the result of the function back to any entity type or complex type.
Open the SchoolModel. Store node and then open the Stored Procedures node. Then right-click the GetCourses stored procedure and select Add Function Import. In the Add Function Import dialog box, under Returns a Collection Of select Entities, and then select Course as the entity type returned.
Store Procedure is faster than the LINQ query. Stored Procedure is good for writing more complex database queries. If there is any change in the database, table, column or datatype then you have to change or Update the Stored Procedure.
Stored procedures are one of the key advantages that the Microsoft SQL server provides. For boosting the query performance, the complex query should be written at the database level through stored procedures. Microsoft . NET Core supports calling of raw query and store procedure through entity framework.
Well stored procedures are not composable. So there is no way to call your SPROC and have the EF automatically populate relationships in the same query, using Include() or something.
So say you have products and categories
and you have a sproc to get Products:
i.e.
var products = context.GetProducts(someproductfilter);
the resulting products won't have their categories loaded.
However if you have a second stored procedure that gets the Categories for said products:
i.e.
var categories = context.GetCategoriesForProducts(someproductfilter);
a feature in EF called relationship fixup, which links related entities once the second entity enters the context, will insure that after both calls are made, each product in products will have a non-null Category.
This is not ideal, because you are doing more than one query, but it will work.
An alternative is to use EFExtensions. The guy who wrote that created the ability to write sprocs that load more data in one go.
Hope this helps
Cheers Alex
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With