2

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?

2
  • 1
    Such a model is no problem with EF. What query are you doing exactly? "When I get Worker from the database..." isn't particularly precise. Commented Sep 11, 2011 at 14:31
  • @Neil Fenwick: I don't know. SpeedBirdNine's answer was good enough. Commented Sep 12, 2011 at 11:27

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I know that using ThirdParty will work. But I need reference to Supplier and not to ThirdParty. I don't want allow reference to Customer.
You can do runtime type identification for this
How and where to use typeof ?? I don't want to support Customer to be attached to Worker.
In set method, check the type, refuse it if typeof(Customer) is true. The ofcourse, the set method for Worker will be accepting ThirdParty instead of Supplier.
1

It sounds like you need to add a Table Per Hierarchy (TPH) definition to your Entity Model definition. (Store all data for derived types in one table)

Here are some links that might help you set that up:

Walkthrough: Mapping Inheritance - Table-per-Hierarchy (Entity Data Model Tools)

How to: Define a Model with Table-per-Hierarchy Inheritance

How to choose an Inheritance Strategy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.