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.
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?
Scaffolding a database produces an Entity Framework model from an existing database. The resulting entities are created and mapped to the tables in the specified database. For an overview of the requirements to use EF Core with MySQL, see Table 7.2, “Connector/NET Versions and Entity Framework Core Support”).
Step 1 − Let's create a new console project with DatabaseFirstDemo name. Step 2 − To create the model, first right-click on your console project in solution explorer and select Add → New Items… Step 3 − Select ADO.NET Entity Data Model from middle pane and enter name DatabaseFirstModel in the Name field.
Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With