2013-03-25 87 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

相關問題