title: Entity Framework Saving Many-to-Many Relationship with Codefirst, Generic Repository, Unit of Work Pattern
body: <p>I'm having a problem getting many-to-many relationships to save correctly using EF Codefirst. I've modeled my classes correctly and used Fluent-API to correctly model the join table. I think the issue has to do with working with a disconnected DTO. When I save changes to the parent entity (Condo), scalular properties on the parent entity (such as Title, and UserId) save correctly, but changes to a child entity (Amenities) do not save to the many to many table. </p>
  
  <p>Here's the code flow that will help clarify things:</p>
  
  <pre><code>public ICommandResult Execute(CreateOrUpdateCondoCommand command)
      {
          ICollection&lt;Amenity&gt; amenities = new List&lt;Amenity&gt;();
  
          foreach (var item in command.Amenities)
          {
              Amenity amenity = new Amenity { AmenityId = item.AmenityId };
              amenities.Add(amenity);
          }
  
          var condo = new Condo
          {
              [...other properties]
              Title = command.Title,               
              Amenities = amenities             
          };                  
  
          if (condo.CondoId == 0)
          {
              condoRepository.Add(condo);
          }
          else
          {
              condoRepository.Update(condo);
          }
  
          unitOfWork.Commit();
  
          return new CommandResult(true);
      }
  
      /// &lt;summary&gt;
      /// Updates the entity.
      /// &lt;/summary&gt;
      /// &lt;param name="entity"&gt;The entity&lt;/param&gt;
      public virtual void Update(T entity)
      {
          dbset.Attach(entity);
          dataContext.Entry(entity).State = EntityState.Modified;
      }
  </code></pre>
  
  <p>I was able to get things working by creating a condoRepository.UpdateCondo(condo) method as follows:</p>
  
  <pre><code>/// &lt;summary&gt;
      /// Method for updating a condo
      /// &lt;/summary&gt;   
      /// &lt;param name="condo"&gt;The condo to update&lt;/param&gt;
      public void UpdateCondo(Condo condo)
      { 
          var updatedCondo = this.DataContext.Set&lt;Condo&gt;().Include("Amenities")
              .Single(x =&gt; x.CondoId == condo.CondoId);
  
          // Set the attributes
          [other properties here...]
          updatedCondo.Title = condo.Title;           
  
          updatedCondo.Amenities.Clear();
  
          foreach (var amenity in condo.Amenities)
          {
              var amenityToAttach = this.DataContext.Amenities.Single(x =&gt; x.AmenityId == amenity.AmenityId);
              updatedCondo.Amenities.Add(amenityToAttach);                
          }
  
          this.Update(updatedCondo);
      }
  </code></pre>
  
  <p>However, is there a better way to do this that is generic and doesn't require me to create a custom "Update" method every time I need to save a many-to-many relationship? This <a href="https://stackoverflow.com/a/11169307/3221076">https://stackoverflow.com/a/11169307/3221076</a> answer helped clarify what I think the problem is, but I'm not sure how to implement a more generic approach.</p>
  
  <p>Thanks,
  Jason</p>