2

我想EF6并试图利用多对多的关系。实体框架6数据库第一对多关系

首先使用数据库这里是我脚本化的数据库。

CREATE TABLE [States] (
    Id int identity (1, 1) not null primary key, 
    Name varchar(50) not null, 
    Abbreviation varchar(2) not null 
) 
GO 

CREATE TABLE Departments (
    Id int identity (1, 1) not null primary key, 
    Name varchar(50), 
) 
GO 

CREATE TABLE [Role] (
    Id int identity (1, 1) not null primary key, 
    Name varchar(50) 
) 
GO 

CREATE TABLE Employees (
    Id int identity (1, 1) not null primary key, 
    FirstName varchar(50), 
    LastName varchar(50), 
    Email varchar(255), 
    DepartmentId int constraint fk_Department_Id foreign key references Departments(Id) 
) 

GO 

CREATE TABLE AssignedRoles (
    Id int identity (1, 1) not null primary key, 
    EmployeeId int not null constraint fk_Employee_Id foreign key references Employees(Id), 
    RoleId int not null constraint fk_Role_Id foreign key references [Role](Id), 
) 
GO 

CREATE TABLE [Addresses] (
    Id int identity (1, 1) not null primary key, 
    EmployeeId int not null, 
    StreetAddress varchar(255), 
    City varchar(55), 
    StateId int not null, 
    ZipCode varchar(10), 
    CONSTRAINT fk_Employee_Id_Address foreign key (EmployeeId) REFERENCES [Employees](Id), 
    CONSTRAINT fk_State_Id foreign key (StateId) REFERENCES [States](Id) 
) 
GO 

我的代码:

public MicroOrmComparison.UI.Models.Employee Add(MicroOrmComparison.UI.Models.Employee employee) 
{ 
    var employeeToInsert = AutoMapper.Mapper.Map<MicroOrmComparison.UI.Models.Employee, Employee>(employee); 
    using (var db = new EmployeeDb()) 
    { 
     db.Employees.AddOrUpdate(employeeToInsert); 
     if (employeeToInsert.Addresses != null) 
     { 
      foreach (var address in employeeToInsert.Addresses) 
      { 
       db.Addresses.AddOrUpdate(address); 
      } 
     } 
     if (employeeToInsert.Roles != null) 
     { 
      foreach (var role in employeeToInsert.Roles) 
      { 
       role.Employees.Add(employeeToInsert); 
       db.Roles.AddOrUpdate(role); 
       db.Employees.AddOrUpdate(employeeToInsert); 
      } 
     } 
     db.SaveChanges(); 
     employee.Id = employeeToInsert.Id; 
    } 
    return employee; 
} 
从EF6数据库第一

生成员工

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated from a template. 
// 
//  Manual changes to this file may cause unexpected behavior in your application. 
//  Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace EntityFramework.DataLayer 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class Employee 
    { 
     public Employee() 
     { 
      this.Addresses = new HashSet<Address>(); 
      this.Roles = new HashSet<Role>(); 
     } 

     public int Id { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string Email { get; set; } 
     public Nullable<int> DepartmentId { get; set; } 

     public virtual ICollection<Address> Addresses { get; set; } 
     public virtual Department Department { get; set; } 
     public virtual ICollection<Role> Roles { get; set; } 
    } 
} 

生成的代码角色

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated from a template. 
// 
//  Manual changes to this file may cause unexpected behavior in your application. 
//  Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace EntityFramework.DataLayer 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class Role 
    { 
     public Role() 
     { 
      this.Employees = new HashSet<Employee>(); 
     } 

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

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

是失败

有罪测试
 [Test] 
    public void ShouldAddRolesToUser() 
    { 
     //Arrange 
     var testUserId = InsertUserToBeModified(); 
     //Act 
     var employee = _employeeRepository.GetFullEmployeeInfo(testUserId); 
     employee.Roles.Add(new MicroOrmComparison.UI.Models.Role 
     { 
      Id = 3, 
      Name = "Supervisor" 
     }); 
     _employeeRepository.Save(employee); 
     //Assert 
     var result = _employeeRepository.GetFullEmployeeInfo(testUserId); 
     result.Roles.Count().Should().Be(1); 
     result.Roles.First().Id.Should().Be(3); 
     //Cleanup 
     _employeeRepository.Remove(testUserId); 
    } 

测试说result.Roles.Count()为0

我的问题是尝试添加到连接表AssignedRoles。我在角色块内的foreach中尝试了多次插入,但仍然没有运气。我已经在这个网站搜索,但仍然没有运气。我一直在使用Micro ORMs,这就是为什么连接表的魔力正在让我大开眼界。任何帮助将不胜感激。如果需要,我有更多的代码,只是让我知道哪些代码不清楚。

当我在foreach循环内调试它没有添加到连接表。 HELP

+0

你是怎样尝试插入到表AssignedRoles? –

+0

这就是我希望foreach(employeeToInsert.Roles中的var角色)代码正在做的事情。 我是新来的实体,并试图添加到db.Roles和db.Employees – EvilToaster101

+0

我没有在您的代码中看到您添加到AssignedRoles表 –

回答

1

编辑

你缺少AssignedRoles表。我将.edmx添加到我的项目中,并且我有这个实体AssignedRole。尝试重新创建你的edmx。

老答案(代码前):

我只是用你的数据库结构和尝试一切工作正常。

EmployeeDbdb = new EmployeeDb(); 

    var empl = new Employee 
     { 
      FirstName = "Test", 
      LastName = "demo", 
      Email = "[email protected]" 
     }; 

     var role = new Role 
     { 
      Name = "Role1" 
     }; 

     db.Roles.AddOrUpdate(role); 

     db.Employees.AddOrUpdate(empl); 
     db.SaveChanges(); 


     db.AssignedRoles.AddOrUpdate(new AssignedRole 
     { 
      EmployeeId = empl.Id, 
      RoleId = role.Id 
     }); 

     db.SaveChanges(); 

OR:

EmployeeDbdb = new EmployeeDb(); 
var empl = new Employee 
{ 
     FirstName = "Test", 
     LastName = "demo", 
     Email = "[email protected]" 
}; 

var role = new Role 
{ 
    Name = "Role1" 
}; 
db.Roles.AddOrUpdate(role); 
db.Employees.AddOrUpdate(empl); 
db.AssignedRoles.AddOrUpdate(new AssignedRole 
{ 
     Role = role, 
     Employee = empl 
}); 
db.SaveChanges(); 
+0

http://imgur.com/vEZZdB1 这是我在尝试执行db.AssignedRoles时所得到的结果,是否存在我缺少的内容?我认为实体应该知道员工和角色之间的关系,并且不需要对AssignedRoles进​​行具体的调用。 – EvilToaster101

+0

就是这样,非常感谢您的帮助! – EvilToaster101

+0

这太好了。如果答案有助于解决问题,请您检查标记。 –