[英]Entity Framework 4.1: How can I access virtual entity properties after creating a new entity?
Here is my simplified code: 这是我的简化代码:
// Domain models
public class Order
{
public int ID { get; set; }
public int CustomerID { get; set; }
public virtual ICollection OrderLineItems { get; set; }
public virtual ICollection Transactions { get; set; }
public void CreatePurchaseDebit()
{
var purchaseDebit = new Transaction()
{
Amount = OrderLineItems.Select(x => x.Product)
.Sum(x => x.Price) * -1
};
Transactions.Add(purchaseDebit);
}
}
public class OrderLineItem
{
public int ID { get; set; }
public int OrderID { get; set; }
public int ProductID { get; set; }
public virtual Product Product { get; set; }
}
public class Product
{
public int ID { get; set; }
public decimal Price { get; set; }
}
// Repository
public class Repository
{
public void Add(Order order)
{
context.Add(order);
order.CreatePurchaseDebit(); // This throws an error
}
}
The problem here is that when attempting to execute CreatePurchaseDebit()
Product
is null
even though ProductID
has been set accordingly by the user during the create process. 这里的问题是,即使用户在创建过程中已相应地设置了
ProductID
,当尝试执行CreatePurchaseDebit()
Product
也会为null
。
My hope was that adding the new Order
to the DbContext
would populate the virtual domain model properties via the associated foreign key ID. 我希望将新的
Order
添加到DbContext
将通过关联的外键ID填充虚拟域模型属性。 In the debugger I can see that Order
has multiple OrderLineItems
and that each OrderLineItem
has a valid ProductID
, however Product
is null
so I can't access the Product
's price! 在调试器中,我可以看到
Order
具有多个OrderLineItems
,每个OrderLineItem
具有有效的ProductID
,但是Product
为null
所以我无法访问Product
的价格!
So is there a way to tell Entity Framework to populate the virtual domain model properties or is my design bad? 那么有没有办法告诉Entity Framework填充虚拟域模型属性,还是我的设计不好?
PS: Even if I call SaveChanges()
after adding adding the new Order to the DbContext Product
is still null
. PS:即使在添加新订单到DbContext
Product
后添加添加,我调用SaveChanges()
仍然为null
。 I want to make sure the Order
has a purchase debit before calling SaveChanges()
so that it is not possible for an order to exist without a transaction. 我想确保在调用
SaveChanges()
之前该Order
具有购买借方,以便没有交易就不可能存在该订单。
Thanks! 谢谢!
Product will not be loaded just because you set up ProductId
. 仅仅因为您设置了
ProductId
就不会加载ProductId
。 If you creating new Order
and you want to have order line items populated with products use products' ids to load products from database and assign them to order line items. 如果您创建新
Order
并希望用产品填充订单行项目,请使用产品ID从数据库加载产品并将其分配给订单行项目。
So instead of doing this: 所以不要这样做:
var order = new Order
{
CustomerId = customerId,
OrderLineItems = new List<OrderLineItme>
{
new OrderLineItem
{
ProductId = productId
}
}
};
use this: 用这个:
var product = context.Products.Single(p => p.Id == productId);
var order = new Order
{
CustomerId = customerId,
OrderLineItems = new List<OrderLineItme>
{
new OrderLineItem
{
Product = product
}
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.