简体   繁体   English

获取 EF TPT 继承中的实体类型

[英]Get type of Entity in EF TPT Inheritance

My domain entities are using EF Table Per Type Inheritance, and here is the relationship.我的域实体正在使用 EF Table Per Type Inheritance,这是关系。

每种类型的表

As you can see Invoices sits on the parent entity Order.cs .如您所见, Invoices 位于父实体Order.cs This is now creating me issues when I deal with invoices.当我处理发票时,这现在给我带来了问题。

For instance, to create an invoice , I need to pass the order Id to the Invoice controller:例如,要创建发票,我需要将订单 ID传递给发票控制器:

public ActionResult Create(int orderId)
{
  if (orderId == 0) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

  // determine whether the order is a SaleOrder or a PurchaseOrder
  // then create the viewModel passing in the SaleOrder / PurchaseOrder details
  return View(viewModel);
}

How do I figure out what child type an order is by simply its Id?如何仅通过其 ID 确定订单的子类型?

UPDATE更新

Theo asked how I was currently retrieving SaleOrders and PurchaseOrders. Theo 问我目前如何检索 SaleOrders 和 PurchaseOrders。 I have this in my context:我在我的上下文中有这个:

public DbSet<PurchaseOrder> PurchaseOrders { get; set; }
public DbSet<SaleOrder> SaleOrders { get; set; }

I have built a repository around each, so I have a PurchaseOrderRepository and a SaleOrderRepository and I do my CRUD that way.我已经围绕每个构建了一个存储库,所以我有一个PurchaseOrderRepository和一个SaleOrderRepository并且我以这种方式执行我的 CRUD。

If the two classes inherit from the same class you can create another DbSet如果两个类继承自同一个类,则可以创建另一个 DbSet

public DbSet<Order> Orders { get; set; }

and in your Action ask for the order like this并在您的 Action 中要求这样的订单

    public ActionResult Create(int orderId)
    {
      if (orderId == 0) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      var order = db.Orders.Find(orderId);
      if(order is SaleOrder){
        //do your stuff for SaleOrder
      }
      if(order is PurchaseOrder){
        //do your stuff for PurchaseOrder
      }          
      return View(viewModel);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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