0

I'm making an application using EF on a legacy database. In the database there are two tables I am concerned with. The structure (in C# form) looks like this:

public class Employee
{
    public int employeeID {get; set;} //primary key
    public string name {get; set;}
    ...//other attributes from this table (not relevant)
}
public class EmployeeProfile
{
    public int profileID {get; set;} //primary key
    public int employeeID {get; set;}
    public string favoritemovie {get; set;}
    ...//other attributes from this table (not relevant)
}

There is a 1 - 1 relationship with EmployeeProfile and Employee in the database. In my application, I'm wanting to create a combined entity, like this:

public class Employee
{
    public int employeeID {get; set;}
    public string name {get; set;}              //taken from Employee Table
    public string favoritemovie { get; set; }   //taken from EmployeeProfile table
}

How can I do this? I've heard of entity splitting but that requires the tables to have the same primary key.

4

2 回答 2

0

EF 已经为您创建了关系。您应该能够通过 Employee 实体访问 EmployeeFile(即 employee.EmployeeProfile[0] 获取与您检索到的员工实体相关的员工资料)

于 2013-03-25T18:06:11.437 回答
0

正如 RandomAsianGuy 所说,从员工实体跳转到个人资料实体很简单。

但是,如果您坚持创建这个合并实体,那么您正在寻找的是 Entity Framework 中的 table-per-type (TPT) 映射继承,使用 Employee 作为基类并从 Employee 派生 EmployeeProfile。

这是使用 EDMX 进行 TPT 继承的 MSDN 演练:

http://msdn.microsoft.com/en-us/data/jj618293

于 2013-03-25T20:57:40.353 回答