实体框架代码第一 - 加载加载不能正常工作? [英] Entity Framework Code First - Eager Loading not working as expected?
问题描述
我有以下Entity Framework POCO类:
public class Customer
我得到
{
public int Id {get; set;}
public string Name {get; set;}
public virtual ICollection< Order>订单{get; set;}
}
public class Order
{
public int Id {get; set;}
public int CustomerId {get ; set;}
public int OrderTypeId {get; set;}
public virtual OrderType类型{get; set;}
public virtual Customer Customer {get; set;} b $ b}
public class OrderType
{
public int Id {get; set;}
public virtual ICollection< Order>问题是,当我返回我的 code> ICollection< Order>Order
好,但OrderType $未填充
Order
的c $ c>属性。我的订单将包含以下详细信息:Id:1
CustomerId:1
客户: b $ b OrderTypeId:3
类型:null //从未返回
如下:
public void ConfigureOrder(ModelBuilder builder)
{
//从订单映射 - > Customer
builder.Entity< Order>()
.HasRequired(x => x.Customer)
.WithMany(x => x.Orders)
.HasConstraint (order,cust)=> order.CustomerId == cust.Id);
//从订单映射 - > OrderType
builder.Entity< Order>()
.HasRequired(x => x.OrderType)
.WithMany(x => x.Orders)
.HasConstraint (order,type)=> order.OrderTypeId == type.Id);
}
我已经禁用了在我的上下文中的延迟加载:public Context(string connectionString)
:base(connectionString)
{
ObjectContext.ContextOptions.LazyLoadingEnabled = false ;
}
为了返回我的仓库中的数据,我使用
包含
方法:System.Data.Entity
的var query = from item in context.Customers
.Include(x => x.Orders)
其中item.Id == customerId
select item;
我假设是因为我不能指定Orders.OrderType
,这是问题,所以我尝试一些变化:
1 - .Include(x => x.Orders.FirstOrDefault()。OrderType)
2 - > .include(Orders)
3 - > .Include(Orders)
.Include(Orders.OrderType)
我不能得到要返回的OrderType属性,除非我直接加载Order:
var query = .Orders
.Include(x => x.OrderType)
选择项目;
此代码将在订单中正确返回OrderType。
解决方案哦亲爱的。看起来我是一头驴子。这是17:45,我现在应该回家了。
我有两个Get方法:
Get(int customerId)
{
//这是我在
内测试的方法var query = from context in context.Customers
.Include(Orders.OrderType)
select item;
}
Get(int customerId,int versionId)
{
//这是运行
的方法var query = .customers
.Include(item.Orders)
select item;
}
因此,
Orders.OrderType
是正确的,虽然看起来很讨厌的解决方案。我需要一些咖啡因。
编辑:
,include的最好方法是使用
System.Data.Entity
的Include方法:.Include(x => x.Orders.Select(o => o.OrderType));
I have the following Entity Framework POCO classes:
public class Customer { public int Id {get;set;} public string Name {get;set;} public virtual ICollection<Order> Orders {get;set;} } public class Order { public int Id {get;set;} public int CustomerId {get;set;} public int OrderTypeId {get;set;} public virtual OrderType Type {get;set;} public virtual Customer Customer {get;set;} } public class OrderType { public int Id {get;set;} public virtual ICollection<Order> Orders {get;set;} }
The problem is that when I return my
ICollection<Order>
I'm getting theOrder
s alright, but theOrderType
property of theOrder
is not being populated. My Order will contain the following detail:Id: 1 CustomerId: 1 Customer: Populated OrderTypeId: 3 Type: null // Never returned
My mapping code looks like the following:
public void ConfigureOrder(ModelBuilder builder) { // Mapping from Order -> Customer builder.Entity<Order>() .HasRequired(x => x.Customer) .WithMany(x => x.Orders) .HasConstraint((order, cust) => order.CustomerId == cust.Id); // Mapping from Order -> OrderType builder.Entity<Order>() .HasRequired(x => x.OrderType) .WithMany(x => x.Orders) .HasConstraint((order, type) => order.OrderTypeId == type.Id); }
I've then disabled Lazy loading in my context:
public Context(string connectionString) : base(connectionString) { ObjectContext.ContextOptions.LazyLoadingEnabled = false; }
So to return the data in my repository I use the
Include
method ofSystem.Data.Entity
:var query = from item in context.Customers .Include(x=> x.Orders) where item.Id == customerId select item;
I was assuming that because I couldn't specify
Orders.OrderType
, that was the problem, so I tried some variations:1 -> .Include(x=> x.Orders.FirstOrDefault().OrderType) 2 -> .Include("Orders") 3 -> .Include("Orders") .Include("Orders.OrderType")
But I can never get the OrderType property to be returned, unless I just load the Order directly:
var query = from item in context.Orders .Include(x=> x.OrderType) select item;
This code will correctly return the OrderType within the order.
解决方案Oh dear. Looks like I was a total donkey. It's 17:45, I should of gone home by now anyway.
I had two Get methods:
Get(int customerId) { // This was the method I was testing within var query = from item in context.Customers .Include("Orders.OrderType") select item; } Get(int customerId, int versionId) { // This was the method that was running var query = from item in context.Customers .Include(item.Orders) select item; }
So,
"Orders.OrderType"
was the correct, albeit nasty looking solution. I need some caffeine.EDIT:
Just coming back to this question, the best way for includes is to use
System.Data.Entity
's Include method:.Include(x=> x.Orders.Select(o=> o.OrderType));
这篇关于实体框架代码第一 - 加载加载不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!