Hi all, I have the following schema in the database:
- BillingReferences (ReferencingType tinyint, ReferencingId tinyint, ReferencedType tinyint, ReferencedId tinyint, IsActive bit) - where all fields (except IsActive) are part of a Unique Index.
- BillingType (BillingTypeId tinyint, Name varchar(50))
ReferencingType and ReferencedType is a foreign key of BillingTypes. BillingTypes contains the following rows:
BillingTypeId | Name
1 | Labels
2 | Countries
3 | PaymentProviders
4 | PaymentOptions
5 | Banks
ReferecingId and ReferencedId represent the Id of one of the following entities (depends on the Referenced/Referencing Type):
- Banks (BankId tinyint, Name varchar(50))
- Countries (CountryId tinyint, Name varchar(50))
- Labels (LabelId tinyint, Name varchar(50))
- PaymentProviders (PaymentProviderId tinyint, Name varchar(50))
- PaymentOptions (PaymentOptionId tinyint, Name varchar(50))
In the future each entity will have some more different columns added but for now this is the schema for simplicity.
There's a connection of (1-) between every entity (except countries) to countries. Labels have a connection of (1-) to Banks, PaymentProviders and PaymentOptions. And PaymentProviders have a connection of (1-*) to PaymentProviders
So for example if I want to connect a bank with BankId 201 to a country with CountryId 3003 I will have a record in BillingReferences that will look like that: ReferencingType = 5 ReferencingId = 201 ReferencedType = 2 ReferencedId = 3003 IsActive = 1
We didn't make a connection/reference table for each type of connection because of extendability consideration - If we want to add another entity all we have to do is add its table and add records for it in BillingReferences and BillingType.
The problem is that I can't configure a conditional foreign key between BillingReferences and each of the entities and I can't seem to configure/map it with EntityFramework either...
I was unable to find any tutorial or example that uses this type of implementation. Am I bound to create a reference table for each connection, or is there a way to configure this with EntityFramework?
Thanks for the help :)