我想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
你是怎樣嘗試插入到表AssignedRoles? –
這就是我希望foreach(employeeToInsert.Roles中的var角色)代碼正在做的事情。 我是新來的實體,並試圖添加到db.Roles和db.Employees – EvilToaster101
我沒有在您的代碼中看到您添加到AssignedRoles表 –