c# - Entity Framework Include 和 Select 正在获取其他一些实体

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

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

另外: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();

Product property on Order

和OrderDetail的issue

Category and Supplier is so weird

我听不懂什么?

最佳答案

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

 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)

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

关于c# - Entity Framework Include 和 Select 正在获取其他一些实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37742497/

相关文章:

javascript - 将数据从 HTTPS 页面发送到本地主机服务器

c# - 尽管我们不能在 xaml 中应用它们,但是否有任何理由使用通用 View 模型?

c# - 为什么通过反射查询程序集的类型会导致程序在调试时意外挂起?

c# - 如何在我的 REST WCF 服务中接受任意 JSON 对象?

c# - ActionResult 到字符串

c# - 强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或 bool 值

c# - 当我创建一个自动属性时,后台会发生什么?

c# - .NET 反编译器区分 "using"和 "try...finally"

javascript - 为什么我的 AJAX 帖子不会重定向到正确的 ActionResult

c# - 如何将多维数组传递给 ASP.NET MVC 3 JsonResult Controller