在EF code杉杉多重继承级别 [英] Multiple Inheritance Levels in EF Code Firs

查看:97
本文介绍了在EF code杉杉多重继承级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何处理继承多个类型。我有一个现有的code这在生产环境中提供现场数据首先/ EF / MVC项目。该系统采用了被称为在其许多功能可以执行文档型,并有可连接至所述DocumentID许多相关联的表。

I am trying to figure out how to handle inheritance for multiple types. I have an existing Code First / EF / MVC project which has live data in a production environment. The system uses a type called a "Document" upon which many functions can be performed, and there are many associated tables that can link to the DocumentID.

有目前作为更加分化的类型的文档,一个购买订单,其中实现了一些更具体的特性,如OrderLines,它引用一个产品,一个数量和单价的集合。还有一些其他领域所特有的采购订单也是如此。在附加于有OrderLines是从基本的文档类型不同,采购订单是供应端的文件,正因为如此,它的第三方是供应商。可能有其他的供给侧文档还具有一个供应商作为未来第三者

There is currently as more differentiated kind of document, a purchase order, which implements some more specific properties, such as a collection of "OrderLines" which reference a Product, a Quantity and a Unit Price. There are some other fields that are specific to the purchase order as well. In additional to being distinct from the base document type in having "OrderLines", the purchase order is a Supply Side document, and as such, it's third party is a "Vendor". There may be other supply side documents that also have a vendor as a third party in the future.

我现在准备实施新的文档类型,这将是需求方,这意味着将有一个第三方的叫做客户。它也有OrderLines的集合

I am now ready to implement a new document type that will be Demand Side, meaning it will have a third party called a customer. It also has a collection of OrderLines.

我的系统目前设置为TPH,所以表称为文档,有一个鉴别列,指出该文件里面有采购订单。

My system is currently set up as TPH, so the table is called "Documents" and there is a discriminator column that says that the documents inside are Purchase orders.

我如何添加新的类型和使用继承来实现OrderLines收集和在客户/客户链接?看来,C#不能做多重继承,所以下面就出了问题:

How can I add the new type and use inheritance to implement the OrderLines collection and the CustomerID / Customer link? It seems that c# cannot do multiple inheritance, so the following would be out of the question:

文档 - > OrderLinesDocument - > PurchaseOrder的

Document -> OrderLinesDocument -> PurchaseOrder

文档 - > OrderLinesDocument - >发票

Document -> OrderLinesDocument -> Invoice

文档 - > VendorDocument - > PurchaseOrder的

Document -> VendorDocument -> PurchaseOrder

文档 - > CustomerDocument - >发票

Document -> CustomerDocument -> Invoice

当中产阶级将执行领域,可能是通用的许多文件但不是所有的文件?

Where the middle class would implement the fields that might be common to many documents but not all documents?

有没有办法做到这一点使用接口,而且还有EF弄不清表是否正确?

Is there a way to accomplish this using interfaces, and still have EF figure out the tables properly?

推荐答案

继承可以在许多方面是有难度的 - 所以我的建议是去'方便'吧。

Inheritance could be tricky in many ways - so my advice is to go 'easy' with it.

但只是在做一个典型的面向对象的设计,这里将产生可用表结构。也许这样的事情可能会为你工作...

But just doing a typical OO design here will produce usable tables, structure. Maybe something like this could work for you...

public class Document
{
    public long ID { get; set; }
    public string Name { get; set; }
}

public class OrderLinesDocument : Document
{
    public ICollection<OrderLine> OrderLines { get; set; }
}

public interface IVendorDocument
{
    ICollection<Vendor> Vendors { get; set; }
}
public class VendorDocument : Document, IVendorDocument
{
    public ICollection<Vendor> Vendors { get; set; }
}

public interface ICustomerDocument
{
    ICollection<Customer> Customers { get; set; }
}
public class CustomerDocument : Document
{
    public ICollection<Customer> Customers { get; set; }
}

public class PurchaseOrder : OrderLinesDocument, IVendorDocument
{
    public ICollection<Vendor> Vendors { get; set; }
}

public class Invoice : Document, ICustomerDocument
{
    public ICollection<Customer> Customers { get; set; }
}

最简单的办法国际海事组织正在使用TPH(只是'是'pretty多)。

The simplest solution IMO is using TPH (just 'as is' pretty much).

但正如我所说,小心它 - 它可能不适合你心里有什么样的工作 - 即你需要进行试验,确保分贝记录看起来最佳。

But as I said, careful with it - and it might not work for what you have in mind - i.e. you'd need to experiment, make sure Db records look optimal.

BTW。您可以使用它像这样,如果你不知道,忘了张贴...

btw. you can use it like this if you didn't know, forgot to post that...

var query1 = db.Documents.OfType<PurchaseOrder>().ToList();
var query2 = db.Documents.OfType<Invoice>().ToList();

var query3 = db.Documents.OfType<CustomerDocument>().ToList();
var query4 = db.Documents.OfType<VendorDocument>().ToList();
var query5 = db.Documents.OfType<OrderLinesDocument>().ToList();

var query6 = db.Documents.OfType<Document>().ToList();

(试试,看看它是如何工作)

(try that to see how it actually works)

这篇关于在EF code杉杉多重继承级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆