gpt4 book ai didi

.net - Entity Framework 何时填充导航属性?

转载 作者:行者123 更新时间:2023-12-01 23:33:52 25 4
gpt4 key购买 nike

我正在使用具有工作单元/存储库模式的 Entity Framework 4.3。在这种情况下,工作单元是一个请求。

工作单元还显式地将延迟加载设置为 true。

然而,我对在这种情况下什么时候延迟加载感到有点困惑,我将不胜感激一些建议。

    var context = Local.Items.Uow.Context; // the context

var r = new ReadRepo<Deal>(context); // the repository

var deals = r.Find(); // IQueryable<Deal>

Rpt_BookmarkedDeals.DataSource = deals.ToList();
Rpt_BookmarkedDeals.DataBind();

存储库 Deals 的导航属性为“Store”。加载 Store 的要求仅在转发器是数据绑定(bind)时才知道。我猜这是加载 nav 属性的地方,但我不确定。

这是执行此操作的最佳方法还是我应该在获得优惠时明确将商店添加为 Include()?

最佳答案

The requirement to load Store is only known when the repeater is databound. I'm guessing this is where the nav property is loaded but I'm not sure.

是的,延迟加载的导航属性在引用该属性时加载,并绑定(bind)到该属性引用它。

Is this the best way of doing this or should I explicitly add Store as an Include() when I get Deals?

没有单一的最佳方法。

如果您的交易链接到 10 个不同的商店,那么延迟加载 deal.Store 将导致 10 个单独的查询被发送到数据库,除了一个获取交易的查询。如果您使用 deals.Include("Store"),一个查询将同时检索交易和商店,但每笔交易都会重复每个商店的数据。哪一个表现更好取决于。

如果您使用延迟加载,如果商店及其交易在加载交易之后但在商店加载之前被删除,则可能会出现不一致。这可以通过使用事务来避免,但在这种情况下,事务必须持续到商店加载完毕。

您需要权衡不同方法的优缺点,以确定哪种方法最适合您的情况。

关于.net - Entity Framework 何时填充导航属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12315931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com