title: Unit of Work/Repository pattern and updating entities using EF
body: <p>I'm trying to get to know UoW pattern a bit better as I like its concept, but currently I'm having a hard times saving entities.</p>
  
  <p>The problem is that when my Edit (POST) action in my controller fires only parent entity gets saved and the child entities are intact i.e. I get the following error:</p>
  
  <blockquote>
    <p>Attaching an entity of type 'Access.Models.ApplicationParameter'
    failed because another entity of the same type already has the same
    primary key value. This can happen when using the 'Attach' method or
    setting the state of an entity to 'Unchanged' or 'Modified' if any
    entities in the graph have conflicting key values. This may be because
    some entities are new and have not yet received database-generated key
    values. In this case use the 'Add' method or the 'Added' entity state
    to track the graph and then set the state of non-new entities to
    'Unchanged' or 'Modified' as appropriate.</p>
  </blockquote>
  
  <p><strong>Note:</strong> Parent entity <strong>Application</strong> can have multiple child <strong>ApplicationParameter</strong> entities.</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(ApplicationVM applicationVM)
      {
          Application application = ViewModelToModel(applicationVM);
          uow.ApplicationRepository.Update(application);
  
          foreach (var applicationParameterVM in applicationVM.ApplicationParameters)
          {
              ApplicationParameter applicationParameter = ViewModelToModel(applicationParameterVM);
  
              bool exists = uow.ApplicationParameterRepository.Exists(applicationParameter.idApplicationParameter);
  
              if (exists)
              {
                  uow.ApplicationParameterRepository.Update(applicationParameter);
              }
              else
              {
                  uow.ApplicationParameterRepository.Insert(applicationParameter);
              }
          }
  
          uow.Save();
          return View(applicationVM);
      }
  </code></pre>
  
  <p><strong>ApplicationParameterRepository.cs (just Update snippet)</strong></p>
  
  <pre><code>    public void Update(ApplicationParameter entity)
      {
          db.Entry(entity).State = EntityState.Modified;
      }
  </code></pre>
  
  <p><strong>UOW.cs</strong></p>
  
  <pre><code>public class UnitOfWork : IDisposable
  {
      private AMEntities db = null;
  
      public UnitOfWork()
      {
          db = new AMEntities();
      }
  
      //Add all the repository handles here
      IApplicationRepository applicationRepository = null;
      IApplicationParameterRepository applicationParameterRepository = null;
      IApplicationUserRepository applicationUserRepository = null;
  
      //Add all the repository getters here
      public IApplicationRepository ApplicationRepository
      {
          get
          {
              if (applicationRepository == null)
              {
                  applicationRepository = new ApplicationRepository(db);
              }
              return applicationRepository;
          }
      }
      public IApplicationParameterRepository ApplicationParameterRepository
      {
          get
          {
              if (applicationParameterRepository == null)
              {
                  applicationParameterRepository = new ApplicationParameterRepository(db);
              }
              return applicationParameterRepository;
          }
      }
      public IApplicationUserRepository ApplicationUserRepository
      {
          get
          {
              if (applicationUserRepository == null)
              {
                  applicationUserRepository = new ApplicationUserRepository(db);
              }
              return applicationUserRepository;
          }
      }
  
      public void Save()
      {
          db.SaveChanges();
      }
  
      private bool disposed = false;
  
      protected virtual void Dispose(bool disposing)
      {
          if (!this.disposed)
          {
              if (disposing)
              {
                  db.Dispose();
              }
          }
          this.disposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  }
  </code></pre>
  
  <p><strong>Question:</strong>  What should I do to have those child entities saved?</p>