EF 6.1代码优先与不同主键的一对一关系 [英] One to One Relationship with Different Primary Key in EF 6.1 Code First

查看:112
本文介绍了EF 6.1代码优先与不同主键的一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用实体框架6.1从PayGroup对象获取对雇员对象的引用时遇到问题.我在PayGroup.SupervisorId-> Employee.EmployeeId上的数据库中有一个外键.请注意,这是零关系或一对一关系(一个薪资组只能有一个主管,而一个雇员只能是一个薪资组的主管).

I am having an issue getting a reference to the employee object from the PayGroup object using Entity Framework 6.1. I have a foreign key in the database on PayGroup.SupervisorId -> Employee.EmployeeId. Note that this is a zero or one-to-one relationship (a pay group can only have one supervisor and an employee can only be the supervisor of one pay group).

根据此GitHub上的帖子,不可能有外来内容具有不同主键的表上的键.我已经将外键手动添加到数据库中,但是我不知道如何设置流利的api映射以便能够从工资组中获取雇员对象.

According to this post on GitHub, it is not possible to have a foreign key on a table with a different primary key. I've added the foreign key to the database manually but I can't figure out how to set up the fluent api mapping to be able to get the employee object from pay group.

付款组表

员工表

注意:数据库中有一个来自PayGroup.SupervisorId-Employee.EmployeeId的外键.

以下是DTO(我目前在这些类之间没有任何工作关系映射):

Below are the DTO's (I don't currently have any working relationship mapping between these classes):

public class PayGroup
{
    public int Id { get; set; }
    public string SupervisorId { get; set; }
    public virtual Employee Supervisor { get; set; }
}

public class Employee
{
    public string EmployeeId { get; set; }
    public string FullName { get; set; }
}

不支持

推荐答案

one-to-one具有显式FK属性(例如您的PayGroup.SupervisorId)的关系.

one-to-one relationship with explicit FK property (like your PayGroup.SupervisorId) is not supported.

因此从模型中删除该属性:

So remove that property from the model:

public class PayGroup
{
    public int Id { get; set; }
    public virtual Employee Supervisor { get; set; }
}

并使用以下流利的映射:

and use the following fluent mapping:

modelBuilder.Entity<PayGroup>()
    .HasRequired(e => e.Supervisor)
    .WithOptional()
    .Map(m => m.MapKey("SupervisorId"));

WithOptional()调用指定了两件事.首先,在Employee类中没有逆向导航属性,其次,FK是可选的(在表中为Allow Nulls = true).

The WithOptional() call specifies two things. First that there is no inverse navigation property in Employee class, and second that the FK is optional (Allow Nulls = true in the table).

如果您决定添加反向导航属性

If you decide to add inverse navigation property

public class Employee
{
    public string EmployeeId { get; set; }
    public string FullName { get; set; }
    public virtual PayGroup PayGroup { get; set; } // <=
}

将其更改为WithOptional(e => e.PayGroup).

如果要使其成为必需(表中的Allow Nulls = false),则使用相应的WithRequiredDependent重载( Dependent ,此处表示Employee将是主要PayGroup将是依赖的).

If you want to make it required (Allow Nulls = false in the table), then use the corresponding WithRequiredDependent overload (Dependent here means that the Employee will be the principal and PayGroup will be the dependent).

这篇关于EF 6.1代码优先与不同主键的一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆