title: Generic repository add duplicates child entities
body: <p>I am having issues using Generic Repository with Entity Framework specifically when it comes to Adding an entity. Here is the repo Interface: </p>
  
  <pre><code> public interface IRepository&lt;TEntity, in TKey&gt; where TEntity : class
   {
     IQueryable&lt;TEntity&gt; GetQueryable();
     IEnumerable&lt;TEntity&gt; GetAll();
     IQueryable&lt;TEntity&gt; Find(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
     TEntity FirstOrDefault(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
     TEntity First(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
     TEntity Single(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
     TEntity SingleOrDefault(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
     void Add(TEntity entity);
     void Attach(TEntity entity);
     void Delete(TEntity entity);
  }
  </code></pre>
  
  <p>And my EfRepository implementation: </p>
  
  <pre><code>public class EFRepository&lt;TEntity,TKey&gt; : IRepository&lt;TEntity,TKey&gt; where TEntity : class
  {
      private readonly DbSet&lt;TEntity&gt; _dbSet;
      private readonly DbContext _context;
  
      public EFRepository(DbSet&lt;TEntity&gt; dbSet,DbContext context)
      {
          _dbSet = dbSet;
          _context = context;
      }
  
      public IQueryable&lt;TEntity&gt; GetQueryable()
      {
          return _dbSet.AsQueryable();
      }
  
      public IEnumerable&lt;TEntity&gt; GetAll()
      {
          return _dbSet;
      }
  
      public IQueryable&lt;TEntity&gt; Find(System.Linq.Expressions.Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return _dbSet.Where(predicate);
      }
  
      public TEntity FirstOrDefault(System.Linq.Expressions.Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return _dbSet.FirstOrDefault(predicate);
      }
  
      public TEntity First(System.Linq.Expressions.Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return _dbSet.First(predicate);
      }
  
      public TEntity Single(System.Linq.Expressions.Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return _dbSet.Single(predicate);
      }
  
      public TEntity SingleOrDefault(System.Linq.Expressions.Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return _dbSet.SingleOrDefault(predicate);
      }
  
      public void Add(TEntity entity)
      {
          _dbSet.Add(entity);
      }
  
      public void Attach(TEntity entity)
      {
  
          _dbSet.Attach(entity);
          _context.Entry(entity).State = EntityState.Modified;
      }
  
      public void Delete(TEntity entity)
      {
          _context.Set&lt;TEntity&gt;().Attach(entity);
          _context.Entry(entity).State = EntityState.Deleted;
      }
  </code></pre>
  
  <p>This is all fine if my DbSet doesnt have any relationships to other tables. However say for the following:</p>
  
  <pre><code>class TopType
  {
      public TopType()
      {
        InnerTypes = new HashSet&lt;InnerType&gt;();
      }
  
      public int id { get; set;}
      public string something { get; set;}
      public virtual ICollection&lt;InnerType&gt; InnerTypes { get; set;}
  }
  
  class InnerType
  {
      public InnerType()
      {
         ChildTypes = new HashSet&lt;ChildType&gt;();
      }
      public int id { get; set;}
      public nullable&lt;int&gt; TopTypeId { get; set;}
      public string somethingElse { get; set;}
  
      public virtual TopType { get; set;}
      public virtual ICollection&lt;ChildType&gt; ChildTypes { get; set;}
  }
  
  class ChildType
  {
      public ChildType()
      {
         InnerTypes = new HashSet&lt;InnerType&gt;();
       }
      public int id { get; set;}
      public string somethingForTheChild { get; set;}
      public virtual ICollection&lt;InnerType&gt; InnerTypes { get; set;}
   }
  </code></pre>
  
  <p>I already have some child types in the db. These are returned to the web client.
  The user of the web client creates a new TopType and then adds as many InnerTypes as the want. For each inner type they can select the child types that already exist in the db and have an id etc populated. The client side code sets up the object correctly AND DOES POPULATE the navigation properties on all types. </p>
  
  <p>On the service layer I have a unit of work that is as follows:</p>
  
  <pre><code>public class WorkoutUnitOfWork : IWorkoutUnitOfWork
  {
      private WorkoutEntities entities;
  
      public WorkoutUnitOfWork()
      {
          entities = new WorkoutEntities();
          entities.Configuration.ProxyCreationEnabled = false;  
      }
  
      private IRepository&lt;TopType, int&gt; topTypeRepository;
      private IRepository&lt;InnerType, int&gt; innerTypeRepository;
      private IRepository&lt;ChildType, int&gt; childTypeRepository;
  
      public IRepository&lt;Workout, int&gt; TopTypeRepository
      {
          get { return topTypeRepository ?? new EFRepository&lt;TopType, int&gt;(entities.TopTypes, entities); }
      }
  
      public IRepository&lt;InnerType, int&gt; InnerTypeRepository
      {
          get { return innerTypeRepository ?? new EFRepository&lt;InnerType, int&gt;(entities.InnerTypes, entities); }
      }
  
      public IRepository&lt;ChildType, int&gt; ChildTypeRepository
      {
          get { return childTypeRepository ?? new EFRepository&lt;ChildType,int&gt;(entities.ChildTypes, entities); }
      }
      public void Commit()
      {
          entities.SaveChanges();
      }
  </code></pre>
  
  <p>When I pass through a populated TopType calling Add on the repository the TopType is added to the database as is the InnerTypes. These are all inserts. The Child types are also inserted. I know this is because the Add method sets all the entity states to added. I also know that I am using a new context for each request. My data access sits in a seperate project to the service that uses it for persistance. I know I need to control the state of the child types to tell the context that they already exist. My question is using the Generic Repository pattern is this possible? Is there some way to search for all child collections and get/test their state.</p>
  
  <p>It feels as though I should be doing this a bit more manaually e.g add the TopType without any of the navigation properties being set and then with the returned Id set up the foreign key on InnerType and then save this creating the necessary entries in a join table. </p>
  
  <p>If this is the only sensible way to do it then the UnitOfWOrk is going to have to stop serving up  the repos and start controlling the access to the Reposiotry classes. </p>
  
  <p>Any advice on known work around's would be great. I don't want to have to step back to named repository implementations if I can help it.</p>