Search code examples
c#entity-frameworkentity-framework-6.1

How do I create a Dictionary<int, EntityState>, and add values inside a for loop?


So, I hope this is simple. I'm coming up with a way to store disconnected entities (due to my case being quite peculiar), and for it to work, I'd like to create a Dictionary with those values inside a for loop.

But I'm getting "An item with the same key" has been added problem, which I do not know why.

I've tried the following:

Dictionary<int, EntityState> StateProduct = new Dictionary<int, EntityState>();

for (int s = 0; s < userProducts.Count; s++ ) //userProducts.Count had value of 3
{
    StateProduct.Add(s, EntityState.Modified);
}

But I get the error:

enter image description here In which:

some probs

I really really do not know what's going on..

Edit: Here is the complete code

var dbIboID = dbs.OrderDB.Where(x => x.OrderID == Order[0].OrderID).FirstOrDefault();

        if(dbIboID.IboID != uid)
        {
            return false;
        }


        //2nd Step:

        //2.0 Attach it. Yes I know it sets it as unchanged. But let me do the magic trick!!!
        dbIboID.OrderProcess = Order.ToList(); //CHANGED
        dbs.OrderDB.Attach(dbIboID);

        //2.1 Extract original values from the database. 

        var originalProducts = dbs.OrderProcessDB.Where(x => x.OrderProcessID == Order[0].OrderProcessID).ToList();
        var userProducts = Order.ToList();

        //This is a dictionary which will be used to set all other entities with their correct states!
        Dictionary<int, System.Data.Entity.EntityState> StateProduct = new Dictionary<int, System.Data.Entity.EntityState>();

        //2.3 Find new added products. addedProducts = userProducts[key] - originalProducts[key]
        if(userProducts.Count > originalProducts.Count)
        {
            for (int i = originalProducts.Count - 1; i < userProducts.Count; i++ )
            {
                StateProduct.Add(i, System.Data.Entity.EntityState.Added);
            }
        }

        //2.3 Find Deleted products = originalProducts - userProducts. Do reverse of the addedProducts
        else
        {
            for (int i = userProducts.Count - 1; i < originalProducts.Count; i++)
            {
                StateProduct.Add(i, System.Data.Entity.EntityState.Deleted);

            }
        }

        //2.4 Find modified products modifiedProducts = [userProducts - addedProducts] different originalProducts


        //This is not 100% fool proof. Because there will be times that I will always have a modification,
        // when objects remained unchanged. 
       for (int s = 0; s < userProducts.Count; s++ )
       {
          StateProduct.Add(s, System.Data.Entity.EntityState.Modified);
       }

        //2.5 Painting Process:

        for (int i = 0; i < dbIboID.OrderProcess.Count(); i++ )
        {
            dbs.DB.Entry(dbIboID.OrderProcess[i]).State = StateProduct[i];
        }

Solution

  • The code as you have shown it should not produce that exception, because the dictionary was allocated immediately prior to the loop, and thus should be empty, and the items being added all are unique integers.

    My guess is that the dictionary already had some values in it. If so, then using Add to set a value will throw an ArgumentException, since the value corresponding to that key can only be replaced, not added, for the Dictionary class only allows one value per key.

    So, if you expect the dictionary not to already have a value for a key, and want an error exception to be thrown if it does, do:

    StateProduct.Add(s, EntityState.Modified)
    

    If you want to add or replace a value, do:

    StateProduct[s] = EntityState.Modified;