Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104801

Entity Framework multi-inheritance?

I have an abstract base class Contact that has two subclasses: Person and Company.

I want to have a Customer, Vendor or other types that can be either a Company or a Person (all sharing the same primary key ContactId).

My question is if it's possible to inherit all these types from Contact? If the answer is no, is there another option of utilizing the Contact property from the PK? What's the recommended design for this scenario?

Note that I want an Employee/Customer etc. to also be able to be a User.

Upvotes: 0

Views: 222

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364389

How would you achieve that in C#? That is the first question you must answer yourselves because .NET doesn't support multi-inheritance so you cannot have single Customer class derived from Person or / and Company - you need separate Customer class derived from Person and another Customer class derived from Company but every time you see this you should know that you are doing something wrong. Also if you in the future find that you need to have Contact which is both Employee and Customer you will be ready to delete whole your application because with inheritance there will be no way to achieve that. Changing Contact from Customer to Employee will be possible only with direct SQL because EF doesn't allow that.

Inheritance is not solution for your problem - you must use composition (relations).

Upvotes: 1

Related Questions