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 sys
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; }
}
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. 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)