rad
rad

Reputation: 1937

[EDMX]Mapping table splitting

i am working with Entity Framework 4.1 with POCO. I would like to map table Employees :

EmployeeID
LastName
FirstName
ManagerID
IsManager

(with ManagerID reflexive association in Employee table)

In

EmployeeBase abstract class contain

EmployeeID
LastName
FirstName

And Employee class (Inherits EmployeeBase) when IsManager is false contain

ManagerID

And Manager class (Inherits EmployeeBase) when IsManager is true

My problem is, in context TT i have just DbSet<EmployeeBase>. How can I generate DbSet<Employee> and DbSet<Manager> ?

Upvotes: 0

Views: 295

Answers (2)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364259

You can't have DbSet for a derived type. It is how an inheritance mapping works. You always have DbSet only for the base type and if you want to run a query only for a sub type you will use OfType extension method.

In your case:

var query1 = context.Employees;                    // returns all employes and managers
var query2 = context.Employees.OfType<Employee>(); // returns only employees
var query3 = context.Employees.OfType<Manager>();  // returns only managers 

Upvotes: 1

Brian Kretzler
Brian Kretzler

Reputation: 9938

There is a detailed explanation to a similar example in this article, the key detail is called "table-per-hierarchy" mapping:

http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx

Upvotes: 1

Related Questions