开发者

[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 开发者_JS百科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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜