title: Entity Framework 6 database first many to many relationship
body: <p>I am trying EF6 and trying to utilize a many to many relationship.</p>
  
  <p>Using Database first here is my scripted out database.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>My Code:</p>
  
  <pre><code>public MicroOrmComparison.UI.Models.Employee Add(MicroOrmComparison.UI.Models.Employee employee)
  {
      var employeeToInsert = AutoMapper.Mapper.Map&lt;MicroOrmComparison.UI.Models.Employee, Employee&gt;(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;
  }
  </code></pre>
  
  <p>Generated Employee from EF6 database first</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     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.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  namespace EntityFramework.DataLayer
  {
      using System;
      using System.Collections.Generic;
  
      public partial class Employee
      {
          public Employee()
          {
              this.Addresses = new HashSet&lt;Address&gt;();
              this.Roles = new HashSet&lt;Role&gt;();
          }
  
          public int Id { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public string Email { get; set; }
          public Nullable&lt;int&gt; DepartmentId { get; set; }
  
          public virtual ICollection&lt;Address&gt; Addresses { get; set; }
          public virtual Department Department { get; set; }
          public virtual ICollection&lt;Role&gt; Roles { get; set; }
      }
  }
  </code></pre>
  
  <p>Generated Code for Role</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     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.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  namespace EntityFramework.DataLayer
  {
      using System;
      using System.Collections.Generic;
  
      public partial class Role
      {
          public Role()
          {
              this.Employees = new HashSet&lt;Employee&gt;();
          }
  
          public int Id { get; set; }
          public string Name { get; set; }
  
          public virtual ICollection&lt;Employee&gt; Employees { get; set; }
      }
  }
  </code></pre>
  
  <p>The Guilty Test that is failing</p>
  
  <pre><code>        [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);
      }
  </code></pre>
  
  <p>The test says result.Roles.Count() is 0.</p>
  
  <p>My issue is trying to add to the join table AssignedRoles. I have tried multiple inserts within the foreach within the role block but still no luck. I have searched within this site but still no luck. I have been working with Micro ORMs which is why the magic of the join table is blowing my mind. Any help would be greatly appreciated. I have more code if needed, just let me know what code is unclear.</p>
  
  <p>When I debug within the foreach loop its not adding to the join table. HELP</p>