简体   繁体   English

有2个表的多对多实体框架

[英]Entity Framework many to many with 2 tables

Scenario 脚本

I'm working with an existing database that contains two tables, which create a many-to-many relationship because they are not properly normalized. 我正在使用包含两个表的现有数据库,这些表创建了多对多关系,因为它们没有正确规范化。 The table structures look like the following. 表结构如下所示。

Employee (table)

 - PersonId
 - JobId
 - JobTitle
 - OfficeLocation
 - EmailAddress

Projects (table)

 - PersonId
 - JobId
 - Supervisor
 - ProjectName

I have the following POCOs: 我有以下POCO:

public class Employee{
    public int PersonId{get;set;}
    public int JobId{get;set;}
    //...
    public virtual ICollection<Project> Projects{get;set;}
}

public class Project{
    public int PersonId{get;set;}
    public int JobId{get;set;}
    //...
    public virtual Employee Employee{get;set;}
}

I'm wiring this up using an EF Fluent API call 我正在使用EF Fluent API调用进行连接

this.HasRequired(p=>p.Employee)
.WithMany(e=>e.Projects)
.HasForeignKey(p=>new {p.PersonId, p.JobId});

Problem 问题

The situation is that some employees have multiple job titles and locations which generates multiple Employee records, and then each employee has multiple projects. 这种情况是,有些员工具有多个职位和职位,这会生成多个Employee记录,然后每个员工都有多个项目。 The problem is that Project.Employee is returning multiple records and generates the following error: 问题是Project.Employee返回多个记录并生成以下错误:

A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. 发生关系多重性约束冲突:EntityReference最多只能有一个相关对象,但是查询返回了多个相关对象。 This is a non-recoverable error. 这是不可恢复的错误。

I've attempted to change the Project class to 我试图将Project类更改为

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

with

this.HasMany(e=>e.Courses)
.WithMany()

But EF expects there to be a 'tweener table relating employee to projects. 但是EF期望有一个“临时工表”,将员工与项目联系起来。

I realize there are problems with the database structure, but at this time I'm left to work with this setup. 我意识到数据库结构存在问题,但是此时我只能使用此设置。 Is it possible to do this type of relationship using Entity Framework or does any have a recommendation on how to tackle this? 是否可以使用Entity Framework建立这种类型的关系,或者有任何关于如何解决这种关系的建议?

My first recommendation would be to fix the schema, but if that is not possible, I ran into a somewhat similar issue on our project and what worked best for us was creating two DBContexts. 我的第一个建议是修复模式,但是如果不可能,我在我们的项目中遇到了一个类似的问题,最适合我们的是创建两个DBContext。 In your case, each context would have a one to many relationship going in each direction. 在您的情况下,每个上下文在每个方向上都有一对多的关系。 Depending on which way you wanted to query the data, either Employee->Project or Project -> Employee would dictate which DBContext you used. 根据您要查询数据的方式,Employee-> Project或Project-> Employee会规定您使用哪个DBContext。 If you are using the fluent API you can keep all of your pocos the same and just have two different mapping config files. 如果您使用的是流畅的API,则可以将所有pocos保持相同,并且只有两个不同的映射配置文件。 Here is a blog post I wrote on the issue. 这是我就此问题撰写的博客文章。

http://blog.mvmiller.net/2013/10/working-with-legacy-database-schemas.html http://blog.mvmiller.net/2013/10/working-with-legacy-database-schemas.html

我建议您为每个关联实体创建一个一对多的关联实体,称其为Employee和Projects一对一的关联实体(EmployeeProjects)PK将为ProjectID + EmployeeID。

looking at your poco class it appears that many to many relation is not well defined Try changing project to public class Project{ public int PersonId{get;set;} public int JobId{get;set;} //... public virtual ICollection <Employee> Employees{get;set;} } 查看您的poco类,似乎多对多关系的定义不明确尝试将项目更改为public class Project{ public int PersonId{get;set;} public int JobId{get;set;} //... public virtual ICollection <Employee> Employees{get;set;} }

changing 
public virtual ICollection&lt;Employee&gt; Employees{get;set;}
</code>

and do check there must be table with name ProjectEmployee or visa versa in DB. 并检查数据库中是否必须有名称为ProjectEmployee的表,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM