简体   繁体   中英

[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> ?

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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM