我正在使用带有 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();
和OrderDetail的issue
我听不懂什么?
最佳答案
如果您不想从表中获取不需要的数据,您可以进行选择。您可以通过匿名选择或使用 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/