0

I'm designing an invoice class. To put it simple:

  • I'm using EF 6 code-first with Fluent-API.
  • The tables(classes): Invoice, Supplier, Customer, Store

The invoice class has an issuer and an addressee. I want something like 'IssuerId' and 'AddresseeId'

The issuer could be either the supplier (for buying items) or the store (for selling/returing items) and the addressee could be either the supplier (for returning items), the customer (for selling items) or the store (for buying items).

Of course I don't want to create FK collumns for each one of 'em, beacause only two would be used for each invoice and the others would be null.

The problem as explained: The addressee and the issuer could be any of those tables for each invoice.

I thought adding an enum as a discriminator to tell EF which table my FK is pointing to:

public int AdresseeId { get; set; }
public AdresseeType AdresseeType { get; set; } // 'C' is customer, 'S' is supplier...

But I need the relationships to work and navigation properties too. I'm trying to find the most clean solution for this case. Any help would be much appreciated. Thanks.

MurariAlex
  • 321
  • 1
  • 3
  • 20
  • 1
    See [here][1] for an excellent description of your problem and possible solutions [1]: http://stackoverflow.com/a/441111/4684493 – Hintham Mar 30 '15 at 14:19

1 Answers1

1

You can use the Table Per Concrete Type inheritance mapping strategy. Create an abstract class Addressee that contains all the properties common to any addressee and then let your Customer, Supplier, and Store entities inherit from it. The collection property on your Invoice will be of type Addressee. In your entity mapping configuration, use the MapInheritedProperties() option.

This article will give you more information and examples. http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines

JC Ford
  • 6,946
  • 3
  • 25
  • 34
  • There is a problem there: supplier and store can be either addressee or issuer, and it's not possible to do multi-inheritance. – MurariAlex Mar 30 '15 at 14:33
  • I think (though I'm not certain) it should work with an interface `IAddressee` instead of an abstract class, in which case you could implement `IIssuer` as well. Just know that table-per-concrete-type has some caveats regarding identity columns that you have to address. – JC Ford Mar 30 '15 at 15:31