C# Entityframework saving record with many to many navigation property
<p>I'm creating my models using ADO.NET (from mysql db). I have 6 tables :</p>

<p>customer, shoppinglist, customer_shoppinglist (that has two FK one for customerid and the other one for shoppinglist id), item and a shoppinglist_item table (that has two FK one for shoppinglist id and the other one for item id)</p>

<p>each shopping list contains a list of items - and each item can be related to several shopping lists.</p>

<p>each customer contains a list of shopping lists- and each shopping list can be related to several customers.</p>

<p>Now I Have 2 problems: </p>

<p>1) I want to add a new customer that has 3 shopping lists. one that exist in the db and two new ones that contains an existing items in their item list.
This is my code (which works well if I'm adding to customer only one new shopping list):</p>

<pre><code>    public static void saveCustomer(Customer customer){
          using (var context = new DBContext()) {
                Customer customerToAdd= new Customer(customer);
                foreach (ShoppingList shoppingList in customer.shoppingLists) {
                    if (shoppingList .id == 0) {
                        customerToAdd.shoppingLists.Add(new ShoppingList(shoppingList));
                    }
                    else
                        customerToAdd.shoppingLists.Add(shoppingList);
                }

                foreach (ShoppingList shoppingList in customer.shoppingLists) {
                    foreach(Item item in shoppingList.items){
                        context.items.Attach(item);
                    }
                    if (shoppingList.id != -1) {
                        context.shoppingLists.Attach(shoppingList);
                    }
                }
                context.customers.Add(customerToAdd);
                context.SaveChanges();
            }
     }
</code></pre>

<p>And </p>

<pre><code> public ShoppingList(ShoppingList oShoppingList) {
        this.id = -1;
        this.name = oShoppingList.name;
        this.description = oShoppingList.description;
    }
</code></pre>

<p>(The new shopping list in customer has id = 0 when function is called)</p>

<p>I get this error :</p>

<pre><code>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
</code></pre>

<p>on line :</p>

<pre><code>context.items.Attach(item);
</code></pre>

<p>2) What should I do if I want to add a new item to the customer shopping list items? </p>