Thursday, July 7, 2011

An Item with the same key already exists

So you are probably thinking what I was thinking.  Thanks, that error is amazingly descriptive.

We all know that this is typically what you would expect to see this if you were using a dictionary and tried to add an already existing item with the same key.  But wait this is MVC3 and all I am trying to do is add a controller.  I have no control over the dictionary that MVCScaffolding (T4) MVC Tooling is using behind the scenes, what do I do now?

Well let me give you some tips on how to diagnose what the issue is and how it came about.  First let me say I have only ran into this when I used Entity Framework that is generated from a database.  The particular database was a newly created database with a couple hundred tables.  The build was at all times successful even though the root cause was related to foreign key constraints in the database.  But let me describe what steps I took to diagnose the issue and determine the resolution.

Background Info:
The controller and associated views I was trying to create were specific to entities located in my edmx file.  I had no business requirements for the particular entities that required a view model so I was building directly from the entity framework generated data context. The MVC3 application would build correctly with no warnings or errors, and I was able to update the entity framework model from the database with no warnings or errors as well.

So what exactly was the problem?
  1. One of the entities had a duplicate foreign key, which I confirmed was defined this way in the database as well.  
  2. Another entity seemed normal and there was no duplication of any key constraints.  But one of the foreign keys was created backwards, meaning that the primary key table was actually the foreign key table and vice versa.
  3. Another entity that caused the issue was due to the fact that the primary key in the one of the constraints was correct but the foreign key was actually pointed to the identity column of the foreign key table.
So how did I discover these were the actual issues?
  1. I removed the entities from the edmx with out deleting them out of the context.store 
    1. This is done by right clicking on the entity
    2. Selecting delete
    3. But when the pop up comes up saying do you with to delete them choosing no.
  2. I then attempted a build knowing that it would throw errors.
  3. When I reviewed the errors I found the duplicate key entry issue, but for the other two issues I was not so lucky to find such an easy to fix mistake as the errors simply stated missing table reference.
After I found the issue I made the correction in the database and updated the model from the database.

Awesome, it will work now ... NOPE!

I wish it were that simple.  Instead, I had to remove the entities from the context.store and then re-add them to the edmx.  For issues 2 and 3, I actually had to fully delete both the affected entity and the related entity that was part of the foreign key constraint from the edmx and add them again.  Fortunately this does not really affect any controllers, views or view models that you have already built unless you ended up renaming a field in the entities.  But you would run into this issue any time you rename a field in an entity.

I hope this help with the dreaded "An item with the same key has already been added" error from MVC3 when attempting to add a controller.

***** UPDATE Mon, Jul 11, 2011 at 9:48 AM *****


Steve Sanderson, Program Manager for .Net Stack at Microsoft has informed me that this issue is related to the MVC Tooling in VS 2010 and has been added to the bug list to be fixed.

*** All Content is provided As Is ***

No comments:

Post a Comment