我可以通过委托给EF中的存储过程来延迟加载导航属性吗? [英] Can I lazy load a navigation property by delegating to a stored procedure in EF?
问题描述
我有以下客户类:
public class Customer
{
public long Id { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
我的数据库有客户和订单表,但没有外键关系。使用存储过程获取客户的订单,该存储过程需要客户ID并返回订单行。我不能修改数据库。
My database has Customers and Orders tables, but no foreign key relationships. Orders for a customer are obtained using a stored procedure that takes the customer ID and returns the order rows. I can't modify the database.
我知道如何从Entity Framework调用存储过程,但是是否可以使用流畅的API配置DbContext,以便访问客户对象的订单集合会延迟通过对存储过程的调用加载实体?
I know how to call the stored procedure from Entity Framework, but, is it possible to configure the DbContext using the fluent API so that accessing the Orders collection of a customer object would lazy load the entities via a call to the stored procedure?
我正在使用最新版本的EF。
I'm using the latest version of EF.
推荐答案
不,你不能。 Lazy加载是在EF创建的代理对象中编码的(如果可能的话),没有办法拦截/配置代理方式的生成方式。
No, you can't. Lazy loading is coded in the proxy object that EF creates (if possible), there's no way to intercept/configure the way proxies are generated.
甚至不可能映射将 DbSet
的默认读取操作存储到存储过程中。它总是一个查询。只有创建,更新和删除可以映射到存储过程。
It's not even possible to map the default read action of a DbSet
to a stored procedure. It's always a query. Only create, update and delete can be mapped to stored procedures.
原因(我认为)存储过程不可组合,所以如果在一个复杂的LINQ查询中,一个实体将映射到一个存储过程(用于读取)可以将查询转换成一个SQL语句。
The reason (I think) is that stored procedures are not composable, so if in a complex LINQ query one entity would be mapped to a stored procedure (for reading) it wouldn't be possible to turn the query into one SQL statement.
这篇关于我可以通过委托给EF中的存储过程来延迟加载导航属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!