Fix for ASP.NET MVC3 Add Controller – An item with the same key has already been added


Just an interesting little find – so I added a new entity to my EDMX and lo and behold I start getting the “An item with the same key has already been added” when I tried to add a controller – anyway the fix was to remove a couple of rougue navigational properties that had been added automatically!!!!

So if you don’t need them and they are causing this problem – remove them

Dom

Advertisements

5 thoughts on “Fix for ASP.NET MVC3 Add Controller – An item with the same key has already been added

    • Duong

      It was a navigational property that pointed to itself (ie The table that the property belonged to). Not sure how it got there but certainly wasn’t any need for it and as soon as I removed it everything worked as expected.

      Let me know if that helps

      Dom

  1. I have a same problem. I’ve solved it. Actually I have a duplicate property with the same name in my ViewModel. One Property was in BaseViewModel and another is in derived Model.

    For Example

    public class BaseviewModel{
    public int UserId { get; set; }
    }

    public class Model : BaseViewModel
    {
    public int UserId { get; set; }
    }
    I have changed them as

    public class BaseviewModel{
    public int UserId { get; set; }
    }

    public class Model : BaseViewModel
    {
    public int User_Id { get; set; }
    }
    Now it is working fine.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s