[英]Entity Framework does not fetch the rows that have a relationship
I have these 2 tables in a SQL Server:我在 SQL 服务器中有这两个表:
Table #1 Order
:表 #1
Order
:
OrderID
Name
IsFetched
Table #2 OrderRows
:表 #2
OrderRows
:
OrderRowId
OrderID
Name
Adress
These tables have a relationship on the OrderID
column: one order can have multiple OrderRows
that are connected to the Order
table by the OrderID
key.这些表在
OrderID
列上有一种关系:一个订单可以有多个OrderRows
,它们通过OrderID
键连接到Order
表。
However, when I query the database using Entity Framework 6, I don't get the OrderRows
that belong to the Order
, they are always null, why is that?但是,当我使用 Entity Framework 6 查询数据库时,我没有得到属于
Order
的OrderRows
,它们总是 null,这是为什么呢? Aren't the OrderRows
supposed to automatically be populated? OrderRows
不是应该自动填充吗?
using (var db = new MyDbEntities())
{
db.Configuration.LazyLoadingEnabled = false;
try
{
var list = db.Order.Where(x => x.IsFetched == null || x.IsFetched == false).ToList();
return list;
}
catch (Exception ex)
{
return null;
}
}
You have to Include the related entities by adding .Include(x => x.OrderRows)
您必须通过添加
.Include(x => x.OrderRows)
来包含相关实体
using (var db = new MyDbEntities())
{
db.Configuration.LazyLoadingEnabled = false;
try
{
var list =
db.Order
.Include(x => x.OrderRows)
.Where(x => x.IsFetched == null || x.IsFetched == false)
.ToList();
return list;
}
catch (Exception ex)
{
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.