0

I am building an ASP.Net MVC 3 web application using Entity Framework 4.1 with the Database First Approach.

I have just setup my solution with separate projects for the following:

  1. UI - MVC app
  2. Model - Class Library
  3. POCOs - Class Library
  4. Repositories - Class Library
  5. Services - Class Library

I have set this up a few times before without any trouble, but now when I try to create a controller in my MVC app, I get the following error:

Unable to retrieve metadata for 'Entityname'. An item with the same key has already been added.

This happens when I try to add any controller with an Entity for its Model class. I am stumped with this. I tried re-creating the model from the db and regenerated my POCO classes, but still this problem exists.

Can anyone please help?

Thanks.

5
  • do you have more than project that has an Entity Model? If so, you will need to make sure that the containers in each have different names. I had this problem in a project where I had a few plug-in dlls that all added a single table to an existing database. I copied and pasted one to another. At run time I got en error like this, until I changed the container name. Commented Mar 15, 2012 at 12:06
  • @Andras - I have several projects within my solution, one of which contains the model. Two of the other projects both reference the model. But I have done this before without any trouble. Commented Mar 15, 2012 at 13:57
  • yeah that's fine - one model is fine; I was talking about multiple projects with their own models. Commented Mar 15, 2012 at 14:03
  • I need to see some code examples if I am going to help you fix this. Commented Mar 16, 2012 at 15:27
  • @wayne - No code to show fella, all that was in the project was a newly created edmx file. The problem was that I renamed a Navigation property within an Entity to a name that already existed within another Entity. No compile error either, just when I tried to add a new controller. It was a tough one to fix. Commented Mar 16, 2012 at 19:50

1 Answer 1

1

I solved it. When Entity Framework created my entity data model, some of the names it gave to the navigation properties were a bit meaningless. I therefore edited a couple of these properties, however, I ended up naming two different navigation properties, with the same type of association, the same name. Needless to say EF didn't like this. I renamed the offending navigation property. It was a tough one to fix as there were no compile errors from the model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.