Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将具有不同主键的两个表映射到一个实体_C#_Entity Framework - Fatal编程技术网

C# 将具有不同主键的两个表映射到一个实体

C# 将具有不同主键的两个表映射到一个实体,c#,entity-framework,C#,Entity Framework,我正在一个遗留数据库上使用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 {

我正在一个遗留数据库上使用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)
}
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
}
与数据库中的
EmployeeProfile
Employee
存在1-1关系。在我的应用程序中,我希望创建一个组合实体,如下所示:

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

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

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

如前所述,从员工实体跳转到档案实体很简单

但是,如果您坚持创建这个合并实体,那么您需要的是实体框架中的每类型表(TPT)映射继承,使用Employee作为基类,并从Employee派生EmployeeProfile

下面是使用EDMX的TPT继承的MSDN演练: