title: Saving an Item in EF with existing childrens
body: <p>Im having some problems saving an object (FeatureType) that have a 1-M relationship with Section.</p>
  
  <pre><code>public class FeatureType
      {
          public int Id { get; set; }
  
          public string Name { get; set; }
  
          [ForeignKey("SectionId")]
          public Section Section { get; set; }
  
          public virtual List&lt;ItemType&gt; ItemTypes { set; get; }
      }
  
      public class Section
      {
          public int Id { get; set; }
  
          public string Name { get; set; }
  
          public int Order { get; set; }
  
          public virtual List&lt;FeatureType&gt; Features { get; set; }
      }
  </code></pre>
  
  <p>If The ItemTypes are new i have no problem and the insert is done correctly. </p>
  
  <p>But if i want to add some existing ItemTypes im getting this Error:</p>
  
  <blockquote>
    <p>An entity object cannot be referenced by multiple instances of
    IEntityChangeTracker.</p>
  </blockquote>
  
  <p>I have been reading about this problem but i havent found a way to solve it, and it might be because of how its designed my application. </p>
  
  <p>Whem im mappinig from my viewModel to my Model, im getting the section ID and getting the section Object from my SectionRepository as this:</p>
  
  <pre><code> private Section GetSection()
          {
              var section = _sectionRepository.GetSection(SectionId);
              return section;
          }
  </code></pre>
  
  <p>And this is what is giving me the problem, as the section is now been tracked by the SectionRepository that have its own context. </p>
  
  <p><strong>How can i solve this? I have tried just creating a new section with the existing ID but it just create me an empty object.</strong></p>
  
  <pre><code>private Section GetSection()
              {
                  var section = new Section{Id=SectionId};
                  return section;
              }
  </code></pre>
  
  <p>UPDATE</p>
  
  <p>To save my entity i just use : </p>
  
  <pre><code>_repository.Create(featureType.ToModel());
  
      public FeatureType ToModel()
          {
              var ft = new FeatureType
                           {
                               Name = Name,
                               ControlType = (ControlType)ControlType,
                               Order = Order,
                               Required = Required,
                               RequiredText = RequiredText,
                               ItemTypes = GetItemTypes().ToList(),
                               Section = GetSection(),
                           };
  
              return ft;
          }
  </code></pre>
  
  <p>UPDATE 2: This is how i have my repositories, i wouldn't like to manage any EF in my controller but with some kind of repository or service.  </p>
  
  <pre><code> public class EFBaseRepository
      {
          protected MyContext Db = new MyContext();
  
          public void Dispose(bool disposing)
          {
              Db.Dispose();
          }
      }
  
   public class EFFeatureTypeRepository : EFBaseRepository, IFeatureTypeRepository
      {
          public IQueryable&lt;FeatureType&gt; GetFeatureTypes
          {
              get { return Db.FeatureTypes.Include("Section").Include("ItemTypes"); }
          }
  
   public Message Create(FeatureType feature)
          {
              try
              {
                  Db.FeatureTypes.Add(feature);
                  Db.SaveChanges();
                  return new Message();
              }
              catch (Exception e)
              {
                  throw;
                  // return new Message(e, string.Format("Error Creating {0}", feature.GetType()));
              }
          }
  
  //..Other Methods
  }
  </code></pre>