gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:45 24 4
gpt4 key购买 nike

我正在使用带有 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/

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