I have ThirdParty entity and two derived entities: Supplier and Customer.
I have another entity called Worker, with Supplier as a member:
public abstract class ThirdParty { }
public class Supplier : ThirdParty { }
public class Customer : ThirdParty { }
public class Worker {
public virtual string Name {get;set;}
public virtual Supplier Supplier {get;set;}
}
When I get Worker from the database using the entity framework I get the following exception:
There are no EntitySets defined for the specified entity type ‘CompanyData.Supplier’. If ‘CompanyData.Supplier’ is a derived type, use the base type instead.
The error tells me to use ThirdParty type instead of Supplier type for the Supplier member. But I want to Supplier to be with Supplier type and not ThirdParty. How can I fix this?
Use a reference(variable) of ThirdParty to store members belonging to both Supplier and Customer (abstract classes cannot have instances but can have references). Any virtual methods of ThirdParty will have implementation in both Supplier and Customer, and any methods that have different implementations for ThirdParty, Supplier and Customer, the appropriate method will be called because of Polymorphism. So receiving them from the DB in a reference of ThirdParty will not cause any problem. Ofcourse there’ll be small problem if there are methods that are not in ThirdParty but in either Supplier or Customer, but again you can always typcast.
Hope this helps.