Can I lazy load a navigation property by delegating to a stored procedure in EF?

拥有回忆 提交于 2019-12-10 03:11:07

问题


I have the following customer class:

public class Customer 
{
    public long Id { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}

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.

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?

I'm using the latest version of EF.


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/26468371/can-i-lazy-load-a-navigation-property-by-delegating-to-a-stored-procedure-in-ef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!