简体   繁体   English

[EDMX]映射表拆分

[英][EDMX]Mapping table splitting

i am working with Entity Framework 4.1 with POCO. 我正在使用POCO与Entity Framework 4.1合作。 I would like to map table Employees : 我想映射表Employees:

EmployeeID
LastName
FirstName
ManagerID
IsManager

(with ManagerID reflexive association in Employee table) (使用Employee表中的ManagerID自反关联)

In

EmployeeBase abstract class contain EmployeeBase抽象类包含

EmployeeID
LastName
FirstName

And Employee class (Inherits EmployeeBase) when IsManager is false contain 当IsManager为false时,Employee类(继承EmployeeBase)包含

ManagerID

And Manager class (Inherits EmployeeBase) when IsManager is true 当IsManager为true时,Manager类(继承EmployeeBase)

My problem is, in context TT i have just DbSet<EmployeeBase> . 我的问题是,在上下文TT我只有DbSet<EmployeeBase> How can I generate DbSet<Employee> and DbSet<Manager> ? 如何生成DbSet<Employee>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 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. 您不能为派生类型使用DbSet 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. 您始终只有基本类型的DbSet ,如果您只想为子类型运行查询,您将使用OfType扩展方法。

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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