简体   繁体   English

实体框架添加现有外键

[英]Entity Framework Adds existing foreign key

I am having a problem with getting a relationship to work how I would like with Entity Framework. 我在与实体框架建立关系方面遇到问题。 The situation is as follows. 情况如下。

There are a number of departments in a store; 商店中有多个部门; food, drink, entertainment, etc. 食物,饮料,娱乐等

There are a number of employees who work in a store. 商店中有许多员工。 Employees are allowed to work in one or more departments. 允许员工在一个或多个部门工作。

The classes I have are: 我的课程是:

public class Department
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<EmployeeDeparmentMapping> DeparmentMappings { get; set; }
}

public class EmployeeDeparmentMapping
{
    public int Id { get; set; }
    public Employee Employee { get; set; }
    public Department Department { get; set; }

}

I am using code first and my modelbuilder looks something like this 我首先使用代码,而我的modelbuilder看起来像这样

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Employee>().HasMany(e => e.DeparmentMappings).WithRequired(e => e.Employee);

        modelBuilder.Entity<EmployeeDeparmentMapping>().HasRequired(edm => edm.Department);
    }

I am having a problem when I try to add EmployeeDeparmentMappings, like so: 尝试添加EmployeeDeparmentMappings时遇到问题,如下所示:

public void SomeMethod(EmployeeDeparmentMapping edm)
{
    MyDbContext myDb = new MyDbContext();
    myDb.EmployeeDepartmentMappings.Add(emd);
    myDb.SaveChanges();
}

The exceptions gets thrown is a PRIMARY KEY violation on the Department table, this is because the department in the EmployeeDepartmentMappings object may already exist in the databse but not in the DbContext. 抛出异常是Department表上的PRIMARY KEY违规,这是因为EmployeeDepartmentMappings对象中的Department可能已经存在于数据库中,而不存在于DbContext中。

I tried the following: 我尝试了以下方法:

public void SomeMethod(EmployeeDeparmentMapping edm)
{
    MyDbContext myDb = new MyDbContext();
    myDb.Departments.Attach(emd.Department);
    myDb.EmployeeDepartmentMappings.Add(emd);
    myDb.SaveChanges();

}

But this throws an InvalidOperationException , the changes get saved to db but the context is apparently broken. 但这会引发InvalidOperationException ,更改将保存到db,但上下文显然已损坏。

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

What am I doing wrong here?? 我在这里做错了什么?

Try: 尝试:

public virtual ICollection<EmployeeDeparmentMapping> DeparmentMappings { get; set; }

and

public virtual Employee Employee { get; set; }
public virtual Department Department { get; set; }

virtual because they are just for navigating purposes they already exist 虚拟的,因为它们仅用于导航目的,它们已经存在

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

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