简体   繁体   English

实体框架4.1:创建新实体后如何访问虚拟实体属性?

[英]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 ,但是Productnull所以我无法访问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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM