[英]Entity Framework 6 database first many to many relationship
我正在尝试EF6,并尝试利用多对多关系。
这里首先使用数据库是我脚本化的数据库。
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测试失败
[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 ORM,这就是为什么联接表的魔力令人震惊的原因。 任何帮助将不胜感激。 如果需要,我还有更多代码,请让我知道尚不清楚哪些代码。
当我在foreach循环中调试时,不会将其添加到联接表中。 救命
编辑 :
您缺少AssignedRoles
表。 我将.edmx添加到我的项目中,并拥有此实体AssignedRole
。 尝试重新创建您的edmx。
旧答案(代码优先):
我只是尝试使用您的数据库结构,并且一切正常。
EmployeeDbdb = new EmployeeDb();
var empl = new Employee
{
FirstName = "Test",
LastName = "demo",
Email = "aa@aaa.com"
};
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();
要么:
EmployeeDbdb = new EmployeeDb();
var empl = new Employee
{
FirstName = "Test",
LastName = "demo",
Email = "aa@aaa.com"
};
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.