best way (if any) to use MvcScaffolding with an EDMX?

Jun 15, 2011 at 2:28 PM

Hello,

I've seen a number of posts here and on the internet written by people like me who would like to use MvcScaffolding on an existing project who makes use of an EF4.1 EDMX model, pointing to an existing DB where EFCodeFirst conventions cannot be enforced. mainly for performace reasons I do not intend to use EFCode First as my main DAL in this project.

If you try and scaffold entities without any trick, in this case, at runtime you get the dreaded:

"The type 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."

As a matter of fact, types generated from EDMX's really do inherit from EntityObject, as the error complains.

What I've tried (unsuccesfully) to make it work:

1) Using MVC3 Powertools -> reverse engineering db to create some codefirst entities. this doesn't seem to work, and even if it would, it would be a pain to regenerate each time and to have duplicate entities types

2) Tried this http://www.programgood.net/2011/03/14/MVC3MvcScaffoldingAndEFModelFirst.aspx with no success, the scaffolding is not done altogether:

Get-PrimaryKey : Cannot find primary key property for type 'XXXX'. No properties appear to be primary keys.
and
Get-ProjectType : Cannot find a type matching the name 'XXXXXContext'. Try specifying the fully-qualified type name, including namespace.

3) deleted everything and tried making POCO classes with http://visualstudiogallery.msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313
same error.

at this point I am clueless, but I am sure there's a proven  way to do this, and you can tell us :)
thanks
Marco
Jul 16, 2011 at 5:51 AM

Hey Marco, this may help you:

http://jameschambers.com/blog/asp.net-mvcscaffolding-generates-extra-properties-in-views

Cheers!

Apr 11, 2012 at 8:36 PM
Edited Apr 11, 2012 at 8:37 PM

Hi, this may be too late for you, but I want to spread the solution everywhere I see someone run into this same problem because it vexed me for months!

You must have a primary key field named "Id" or "<ClassName>Id". The easiest thing to do is to use the designer to change the name of the primary key field to either "Id" or "<ClassName>Id".

This worked for me and now I can scaffold without any hiccups!!!