加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 实体框架中导航属性的问题

发布时间:2020-12-16 01:42:40 所属栏目:百科 来源:网络整理
导读:当我执行此查询时,我可以在TypeP属性中导航: var items = from item in context.ProductosBodegas.Include("Product.TypeP") select item; 但是当我执行此查询时,TypeP属性为null: var items = from item in context.ProductosBodegas.Include("Product.Ty
当我执行此查询时,我可以在TypeP属性中导航:

var items = from item in context.ProductosBodegas.Include("Product.TypeP")
            select item;

但是当我执行此查询时,TypeP属性为null:

var items = from item in context.ProductosBodegas.Include("Product.TypeP")
            select item.Product;

为什么是这样?

解决方法

看起来Include只会影响直接返回的对象:

http://msdn.microsoft.com/en-us/library/bb896272.aspx

否则你可以打电话

item.TypePReference.Load()

但是,如果在循环中使用,这可能(并且将会)导致性能问题(N 1选择).

假设Product和ProductosBodegas之间的关系是双向的,那么另一种选择是“反转”您的查询:

var items = context.Products
    .Include("TypeP")
    .Where(p => p.ProductosBodegas.Any( /* include condition here if needed */ ))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读