在实体框架中使用存储过程,如何让实体填充其导航属性?

时间:2022-03-14 02:17:01

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?

有工作吗?

2 个解决方案

#1


19  

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.

存储过程不可组合。因此,无法使用Include()或其他方法调用您的SPROC并让EF自动在同一查询中填充关系。

So say you have products and categories

所以说你有产品和类别

and you have a sproc to get Products:

你有一个sproc来获得产品:

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.

EF中称为关系修正的功能,在第二个实体进入上下文后链接相关实体,将确保在两次调用之后,产品中的每个产品都具有非空类别。

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.

另一种方法是使用EFExtensions。写这篇文章的人创造了编写sprocs的能力,可以一次加载更多数据。

Hope this helps

希望这可以帮助

Cheers Alex

干杯亚历克斯

#2


4  

I found this SO question when researching Stored Procedures (SPs) with EF. I also see people like @KristianNissen and @Todilo have asked if there is an update with EF6.

在使用EF研究存储过程(SP)时,我发现了这个问题。我也看到像@KristianNissen和@Todilo这样的人问过EF6是否有更新。

The answer is yes, EF 6 has changed things, but it did not add anything to help load navigational properties when using SPs. Nor can you use the .Include() method with SPs as was asked in this SO question.

答案是肯定的,EF 6改变了一些东西,但在使用SP时它没有添加任何东西来帮助加载导航属性。你也不能像在这个SO问题中那样使用带有SP的.Include()方法。

The only way is to write your SP to specifically load the navigational properties. However there is now some good Microsoft documentation on using SPs - see Query SP and SP returning multiple result sets.

唯一的方法是编写SP以专门加载导航属性。但是现在有一些关于使用SP的好的Microsoft文档 - 请参阅Query SP和SP返回多个结果集。

For completeness the change that EF version 6 brought in was to allow Stored Procedures (SPs) to handle insert, update and delete - see Microsoft article and Entity Framework Tutotial.

为了完整起见,EF版本6引入的更改是允许存储过程(SP)处理插入,更新和删除 - 请参阅Microsoft文章和实体框架Tutotial。

#1


19  

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.

存储过程不可组合。因此,无法使用Include()或其他方法调用您的SPROC并让EF自动在同一查询中填充关系。

So say you have products and categories

所以说你有产品和类别

and you have a sproc to get Products:

你有一个sproc来获得产品:

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.

EF中称为关系修正的功能,在第二个实体进入上下文后链接相关实体,将确保在两次调用之后,产品中的每个产品都具有非空类别。

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.

另一种方法是使用EFExtensions。写这篇文章的人创造了编写sprocs的能力,可以一次加载更多数据。

Hope this helps

希望这可以帮助

Cheers Alex

干杯亚历克斯

#2


4  

I found this SO question when researching Stored Procedures (SPs) with EF. I also see people like @KristianNissen and @Todilo have asked if there is an update with EF6.

在使用EF研究存储过程(SP)时,我发现了这个问题。我也看到像@KristianNissen和@Todilo这样的人问过EF6是否有更新。

The answer is yes, EF 6 has changed things, but it did not add anything to help load navigational properties when using SPs. Nor can you use the .Include() method with SPs as was asked in this SO question.

答案是肯定的,EF 6改变了一些东西,但在使用SP时它没有添加任何东西来帮助加载导航属性。你也不能像在这个SO问题中那样使用带有SP的.Include()方法。

The only way is to write your SP to specifically load the navigational properties. However there is now some good Microsoft documentation on using SPs - see Query SP and SP returning multiple result sets.

唯一的方法是编写SP以专门加载导航属性。但是现在有一些关于使用SP的好的Microsoft文档 - 请参阅Query SP和SP返回多个结果集。

For completeness the change that EF version 6 brought in was to allow Stored Procedures (SPs) to handle insert, update and delete - see Microsoft article and Entity Framework Tutotial.

为了完整起见,EF版本6引入的更改是允许存储过程(SP)处理插入,更新和删除 - 请参阅Microsoft文章和实体框架Tutotial。