2013-03-25 90 views
0

我正在使用遗留数据库上的EF进行应用程序。在数据库中有两个我关心的表。 (在C#形式)的结构是这样的:将具有不同主键的两个表映射到一个实体

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) 
} 

有一个1 - 与数据库EmployeeProfileEmployee 1的关系。在我的应用程序中,我想创建一个组合实体,如下所示:

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 
} 

我该怎么做?我听说过实体拆分,但这要求表具有相同的主键。

回答

0

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

0

As RandomAsianGuy指出,从员工跳转到配置文件实体实体。

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

下面是使用TPT继承的MSDN步行通过EDMX:

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

相关问题