Friday, July 15, 2011

Error using MvcScaffolding with EF4.0

scaffoldingIf you are just starting out with the MvcScaffolding Nuget package to scaffold your basic CRUD operations on an MVC3 project, you should know that it requires EF 4.1 to run correctly without the following error.

The type 'LPM.Project' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject

If you already ran the scaffolding via the Package Manager Console using syntax similar to the following, PM>scaffold controller <table> –Repository –Force, to resolve the error, I needed to:

  1. Delete the scaffolded controller files and view files from the project.
  2. Install the Entity Framework 4.1
  3. Re-Generate the Database From Model (assuming that is how you created the Entities to begin with)
  4. Right click on the .edmx Design surface, click Add Code Generation Item.
  5. Click the Code link in the Installed Templates tree. Select ADO.Net DbContext Generator. Name the code generation template, it ends with a .tt extension.
  6. Rebuild the project.
  7. Re-scaffold the tables.
  8. Run the project

Steps 4 & 5 above were the key to getting it to work after installing the Entity Framework 4.1 and with all my combinations of re-scaffolding, re-generating the model, re-creating the model, etc. That important bit of information came from MVC 3 Scaffolding issue with Entity Framework.

If you look at the Model.designer.cs (or .vb) code prior to adding the ADO.Net DbContext Generator, you will see that the Model entities do inherit from the EntityObject (which is noted in the error message as a potential cause of the exception). Adding the ADO.Net DbContext Generator disables the default code generation and inherits from the DbContext instead.

more information:

No comments: