title: EF Inserting Duplicate Records in Many to Many tables
body: <p>I am quite confused on this one.</p>
  
  <p>I have read many articles such as:</p>
  
  <p><a href="https://stackoverflow.com/questions/15262974/entity-framework-adding-record-into-many-to-many-mapping-table">Entity Framework adding record into many to many mapping table</a></p>
  
  <p>and my situation matches what is described in these articles. In essence I have a typical many to many scenario. I have a customer table, a fixed address table, and CustomerFixedAddress. As you might guess the CustomerFixedAddress table is two columns. The CustomerId and the FixedAddressId.</p>
  
  <p>Just for understanding the Fixed Address is a list of pre-qualified addresses the user selects from.</p>
  
  <p>My problem is that I am not understanding something in the articles because my efforts are still resulting in records being entered in two tables.</p>
  
  <p>My most recent attempts went along these lines from one of the articles to get use an object that is a replica / copy of the existing record. (My poor assumption was that EF would recognize there is an existing record and not create a new one)</p>
  
  <p>I don't <em>KNOW</em> this to be a part of the problem but I do wonder if my mapping is incorrect so here it is for the CustomerMap:</p>
  
  <pre><code>this.HasMany&lt;FixedAddress&gt;(ef =&gt; ef.FixedAddress)
      .WithMany()
      .Map(m =&gt; m.ToTable("Customer_FixedAddress")
      .MapLeftKey("Customer_Id").MapRightKey("FixedAddress_Id"));
  </code></pre>
  
  <p>So basically here is what I do:</p>
  
  <pre><code>//This line passes the id of user selected address record to our address service
  //which returns an object that is exactly the record I want the relationship pointing to
  FixedAddress FxdAddy = _addyService.GetFixedAddressesById(model.FixedAddyId);
  
  //So here I am adding the addy record to the customer object
  customer.FixedAddresses.Add(FxdAddy);
  </code></pre>
  
  <p>The result is my Customer table is untouched.
  My FixedAddress table has a new record inserted....that is a dupe of the existing record the user selected.</p>
  
  <p>My CustomerFixedAddress table has a new entry that is correct and incorrect. The customerid column is correctly populated. The fixedaddress column is correctly populated with the id of the <strong><em>NEW</em></strong> dupe record that was inserted.</p>
  
  <p>That is not what I want. The only table that should have a record inserted is CustomerFixedAddress table...and obviously the new record would have the proper customerid and fixedaddressid of the <strong><em>EXISTING</em></strong> records in the respective tables. </p>
  
  <p>So I kinda understand how the dupe is added to the fixedaddy table...I've added it to the customer object and then ef updates everything from that perspective...but I don't know how to do it differently to get the result I want???</p>
  
  <p>Thank You</p>
  
  <hr>
  
  <p>@slauma this project was developed by people far more knowledgeable so my apologies for hacking through it.</p>
  
  <p>In the addy service I just fire off some LINQ as such:</p>
  
  <pre><code>public FixedAddress GetFixedAddressesById(int FxdAddyId)
  {
    FixedAddress FxdAddy = new FixedAddress();
  
    var query = _addressFixedAddressRepository.Table
              .Where(efb =&gt; efb.Id == FxdAddyId)
              .Select(...removed save space);
          foreach (var item in query)
          {...removed save space }
  
          return FxdAddy;
   }
  </code></pre>
  
  <p>Now I think the follow up would be what context is that _addressFixedAddressRepository?</p>
  
  <p>It is defined class wide as:</p>
  
  <pre><code>private readonly IRepository&lt;FixedAddress&gt; _addressFixedAddressRepository;
  </code></pre>
  
  <p>and brought into existence in the constructor where it gets passed in then assigned locally.....so is the customer object. </p>
  
  <p>Could it be that I should update the customer object in the addyservice? I say this just guessing that the customer object is in a different context when accessed from the controller..(what I am doing now) thus the return from the service is different in it's eyes.</p>
  
  <hr>
  
  <p>Ok so further I found in the service the </p>
  
  <pre><code>private readonly IDbContext _context;
  </code></pre>
  
  <p>and of course it is injected on the constructor.</p>