【问题标题】:Entity Framework Include and Select are gettind some other entitiesEntity Framework Include 和 Select 是一些其他实体
【发布时间】:2016-10-11 01:59:03
【问题描述】:

我正在使用带有 Northwind 数据库的 Entity Framework 6 最新稳定版本。我写了一个如下的查询。即使我没有包括客户来订购。并且还包括实体 OrderDetails 有 Order(这就像递归)。最终包含的 OrderDetails 实体 Product 具有 Category,即使我没有包含在内。奇怪的是 Supplier 是一个导航属性,但它对于产品是 null 。

另外:LazyLoading 和 ProxyCreationEnabled 为 false

var orders = Context.Orders
            .Include(i => i.Order_Details)
            .Include(i => i.Order_Details.Select(a => a.Product))
            .Where(i => i.EmployeeID == employeeId && i.CustomerID == customerId)
            .ToList();

还有 OrderDetail 的问题

我听不懂什么?

【问题讨论】:

  • 如果供应商为空,则表示您的存储库中不会存在数据
  • 因为我不想要供应商,所以我也不想要类别。如您所见,查询中没有包含
  • 好吧,你没有Include供应商,我们不知道是否一切都设置正确以支持供应商的延迟加载。可能不是。您必须出示您的地图,我们才能对此发表任何看法。

标签: c# .net asp.net-mvc entity-framework linq


【解决方案1】:

如果您不想从表中获取不需要的数据,您可以进行选择。您可以通过匿名选择或使用视图模型类来完成

 var orders = Context.Orders
            .Include(i => i.Order_Details)
            .Include(i => i.Order_Details.Select(a => a.Product))
            .Where(i => i.EmployeeID == employeeId && i.CustomerID == customerId)
            .Select new
             {
                //here you can select the fields which all are you required
             }

或

            .Select new requireddatavm
             {
                //here you can select the fields which all are you required
             }

在方法语法中,与其他运算符:

.Where(i => i.EmployeeID == employeeId && i.CustomerID == customerId)

,返回包括包含字段在内的整行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多