EF 6.1 Code First 中与不同主键的一对一关系

One to One Relationship with Different Primary Key in EF 6.1 Code First(EF 6.1 Code First 中与不同主键的一对一关系)
本文介绍了EF 6.1 Code First 中与不同主键的一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

根据

员工表

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

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

公共类 PayGroup{公共 int ID { 获取;放;}公共字符串 SupervisorId { 获取;放;}公共虚拟员工主管{获取;放;}}公共类员工{公共字符串 EmployeeId { 获取;放;}公共字符串 FullName { 获取;放;}}

解决方案

一对一 与显式 FK 属性(如您的 PayGroup.SupervisorId)的关系不是支持.

所以从模型中删除该属性:

公共类 PayGroup{公共 int ID { 获取;放;}公共虚拟员工主管{获取;放;}}

并使用以下流畅的映射:

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

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

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

公共类员工{公共字符串 EmployeeId { 获取;放;}公共字符串 FullName { 获取;放;}公共虚拟 PayGroup PayGroup { 获取;放;}//<=}

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

如果你想让它成为必需的(Allow Nulls = false在表中),那么使用对应的WithRequiredDependent重载(Dependent这里意味着 Employee 将是 principalPayGroup 将是 dependent).

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

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.

Pay Group Table

Employee Table

Note: There is a foreign key from PayGroup.SupervisorId - Employee.EmployeeId in the database.

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 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"));

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; } // <=
}

change it to WithOptional(e => e.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 Code First 中与不同主键的一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)