<div class="post-header"> <div class="post-header-line-1"></div> </div> <div class="post-body entry-content">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?
</div> <div class="post-body entry-content"> </div> <div class="post-body entry-content">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.
</div> <div class="post-body entry-content"> </div> <div class="post-body entry-content">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.
</div> <div class="post-body entry-content"> </div> <div class="post-body entry-content">So what exactly was the problem?</div> <div class="post-body entry-content">
One of the entities had a duplicate foreign key, which I confirmed was defined this way in the database as well.
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.
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.
</div>
So how did I discover these were the actual issues?
I removed the entities from the edmx with out deleting them out of the context.store
This is done by right clicking on the entity
Selecting delete
But when the pop up comes up saying do you with to delete them choosing no.
I then attempted a build knowing that it would throw errors.
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 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.
Wow. Thanks so much! I'm a veteran developer, but new to the MS development environment. That would have taken me a long time to figure out. My problem was a duplicate key. I removed the extra key, then re-created my models and it resolved the issue.
(I had been thinking that my table was somehow conflicting with an internal MVC3 table somehow, so I was far off the track.)
skcookie
Member
534 Points
138 Posts
An Item with the same key already exists
Jul 08, 2011 07:43 PM|LINK
An Item with the same key already exists
<div class="post-header"> <div class="post-header-line-1"></div> </div> <div class="post-body entry-content">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?
</div> <div class="post-body entry-content"> </div> <div class="post-body entry-content">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.
</div> <div class="post-body entry-content"> </div> <div class="post-body entry-content">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.
</div> <div class="post-body entry-content"> </div> <div class="post-body entry-content">So what exactly was the problem?</div> <div class="post-body entry-content">
- One of the entities had a duplicate foreign key, which I confirmed was defined this way in the database as well.
- 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.
- 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.
</div>So how did I discover these were the actual issues?
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 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 ***
Patrick P.
Microsoft Certified Professional
Remember to mark as answer where appropriate!
queej
Member
2 Points
1 Post
Re: An Item with the same key already exists
Jan 06, 2012 02:58 PM|LINK
Wow. Thanks so much! I'm a veteran developer, but new to the MS development environment. That would have taken me a long time to figure out. My problem was a duplicate key. I removed the extra key, then re-created my models and it resolved the issue. (I had been thinking that my table was somehow conflicting with an internal MVC3 table somehow, so I was far off the track.)
skcookie
Member
534 Points
138 Posts
Re: An Item with the same key already exists
Feb 25, 2012 01:50 PM|LINK
glad it was able to help you.
Patrick P.
Microsoft Certified Professional
Remember to mark as answer where appropriate!
maxpayn2
Member
10 Points
9 Posts
Re: An Item with the same key already exists
Mar 04, 2012 08:28 AM|LINK
Thanx alot , I have searched 3 days , your thread realy help me , I delete the Entity Data Model and re-Add it