11
votes

I'm having problems with Visual Studio 2012, trying to add an MVC4 Controller with scaffolding. Ideally I would like to have an Entity Framework model (edmx file, etc) generated from a database using Add New Item - ADO.NET Entity Data Model (i.e. not Code First) in a separate assembly from my web application. However, when I set this up and use Add Controller, specifying "MVC controller with read/write actions and views, using Entity Framework" and choosing a Model class and Data context class from my DatabaseModel assembly the following alert message pops up.

'Ifl.Payforit4.DatabaseModel.Mno' is not part of the specified 'Ifl.Payforit4.DatabaseModel.Payforit4Entities' class, and the 'Ifl.Payforit4.DatabaseModel.Payforit4Entities' class could not be modified to add a 'DbSet' property to it. (For example, the 'Ifl.Payforit4.DatabaseModel.Payforit4Entities' class might be in a compiled assembly.)

Not being able to modify the class makes sense since it is in another assembly, although in the same solution, and auto-generated via T4 but looking at the auto-generated code for Payforit4Entities the 'DbSet' property is quite clearly there already.

    public DbSet<Mno> Mnoes { get; set; }

I've tried a number of other things.

  1. putting the data model directly in the web application
  2. changing the model class to a variety of other tables in the database in case there is a problem with the Mno class
  3. reducing the data model to just a single, simple table
  4. using the Entity Framework Power Tools Beta 2 to reverse engineer a Code First model. This generated a new set of errors. I can see why it is a beta.
  5. changing the ADO.NET Data Model Code Generation Strategy from None to Default to create a data model based on ObjectContext rather than DbContext
  6. Turning off pluralisation so the property name is Mno instead of Mnoes

None of them worked. The only thing that did work was writing a Code First DbContext-derived class and POCO by hand. Coincidentally every example I've found that demonstrates MVC4 scaffolding uses this sort of data model. Is there something somewhere that says Code First is the only sort of data model that works with MVC4 scaffolding? Has anyone managed to scaffold a database first (.edmx) data model in Visual Studio 2012? The database is complex enough that I'd rather stick with a database first strategy.

I can see that there would have to be some differences in the scaffolding of Code First versus Database First models. For example, the former has the POCO property holding the key indicated by a KeyAttribute whereas the latter holds that information in the edmx model files. Is this the rationale for the reverse engineer feature in Entity Framework Power Tools? Are we expected to move away from edmx files to reverse engineered Code First models in order to use MVC4 scaffolding? If so, are we expected to carry on using Dynamic Data projects until the Entity Framework Power Tools are finished?

4

4 Answers

13
votes

The trick is to first compile your solution and then type in the context class manually. Don't choose it from the Dropdown list, just type in the class name by yourself and it will magically work ;-)

See here: ASP.NET MVC4– How to use a Database First EF in a MVC controller

0
votes

I don't think we can scaffold from edmx files with Entity Framework 5.

I've tried a similar set of things and am still hitting errors.

There are a couple of suggestions that maybe just deleting a bad edmx and starting again could generate the correct .tt files to allow the scaffolding to work correctly, but I've not yet seen this.

I'm going to try some more to get it working, as I prefer DB first approaches. I will update this answer if I get any further.

0
votes

The edmx code generator in Entity Frameork 5/VS 2012 is buggy. It creates code that is full of compiler errors. I'm guessing it wasn't ready for shipping when vs2012 was released, which is why they set the default code generation flag to none.

The only question for Microsoft is: when can we expect an update that fixes this?

-1
votes

It's truth! The trick is to first compile your solution and then type in the context class manually. Don't choose it from the Dropdown list, just type in the class name by yourself and it will magically work ;-)

thank!! Marcus!