使用代码首先在实体框架问题建模关系 [英] Problem modelling relationship in Entity Framework using code first

查看:148
本文介绍了使用代码首先在实体框架问题建模关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想先在实体框架内学习码时遇到了问题建模的关系。这是一个基本的人力资源数据库,这对于这个份上有两个实体,员工和部门。

I'm trying to learn code first within the Entity Framework and am having trouble modelling a relationship. It's a basic HR database which for the sake of this has two entities, Employees and Departments.

该员工属于一个部门,该部门拥有一支由管理员和经理,他们两人都在影响员工。我已经试过这使用以下模型:

The Employee belongs to a department and the department has a Team Administrator and a Manager, both of whom are in effect employees. I've tried to model this using the following:

EMPLOYEE

public int? DepartmentID { get; set; }
public virtual Department Department { get; set; }

Context:

modelBuilder.Entity<Employee>().HasOptional(x => x.Department);

DEPARTMENT

public class Department
{
    [Required]
    public int DepartmentID { get; set; }

    [Required(ErrorMessage = "The description is required.")]
    public string Description { get; set; }

    public int? ManagerID { get; set; }
    public virtual Employee Manager { get; set; }

    public int? TeamAdministratorID { get; set; }
    public virtual Employee TeamAdministrator { get; set; }
}

Context:

modelBuilder.Entity<Department>().HasOptional(x => x.Manager);
modelBuilder.Entity<Department>().HasOptional(x => x.TeamAdministrator);



显然,我希望Department表只具有四列 - 的DepartmentID,说明,经理ID和TeamAdministratorID但它产生一个额外的两个用于关系,即Manager_EmployeeID和Team_Administrator_EmployeeID。此外,在雇员表中的列Department_DepartmentID生成到存储中的DepartmentID,而不是它使用我在实体规定的DepartmentID列。

Obviously I would want the Department table to have only four columns - DepartmentID, Description, ManagerID and TeamAdministratorID but it is generating an extra two for the relationship, namely Manager_EmployeeID and Team_Administrator_EmployeeID. Also, in the Employee table the column Department_DepartmentID is generated to store the DepartmentID instead of it using the DepartmentID column I specified in the entity.

我在做什么错了?我如何需要定义字段和关系,以避免代码先不理会我指定并生成它的数据库中的自己的导航领域?

What am I doing wrong? How do I need to define the fields and relationships to avoid having code first ignore what I specify and generate it's own navigation fields in the database?

推荐答案

这是因为你的车型配置是不完整的 - 你流利的API开始自己的映射,所以你必须告诉EF这些属性的确是关系FKS。对于员工使用:

That because your model configuration is incomplete - you started your own mapping with Fluent API so you must tell EF that these properties are indeed FKs for relations. For employee use:

modelBuilder.Entity<Employee>()
            .HasOptional(x => x.Department)
            .WithMany()
            .HasForeignKey(x => x.DepartmentID);

和供部门使用:

modelBuilder.Entity<Department>()
            .HasOptional(x => x.Manager)
            .WithMany()
            .HasForeignKey(x => x.ManagerID);
modelBuilder.Entity<Department>()
            .HasOptional(x => x.TeamAdministrator);
            .WithMany()
            .HasForeignKey(x => x.TeamAdministratorID);



顺便说一句。而对关系的对面集合导航属性将难以用模型(均 WithMany 是空的)。至少应该有:

Btw. without collection navigation properties on opposite side of relations it will be hard to use model (all WithMany are empty). At least Department should have:

public virtual ICollection<Employee> Employees { get; set;}

和映射应该被修改为:

modelBuilder.Entity<Employee>()
            .HasOptional(x => x.Department)
            .WithMany(y => y.Employees)
            .HasForeignKey(x => x.DepartmentID);

这篇关于使用代码首先在实体框架问题建模关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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