title: MVC4 scaffolding of database-first Entity Framework model in Visual Studio 2012
body: <p>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.</p>
  
  <blockquote>
    <p>'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.)</p>
  </blockquote>
  
  <p>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.</p>
  
  <blockquote>
  <pre><code>    public DbSet&lt;Mno&gt; Mnoes { get; set; }
  </code></pre>
  </blockquote>
  
  <p>I've tried a number of other things.</p>
  
  <ol>
  <li>putting the data model directly in the web application</li>
  <li>changing the model class to a variety of other tables in the database in case there is a problem with the Mno class</li>
  <li>reducing the data model to just a single, simple table</li>
  <li>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.</li>
  <li>changing the ADO.NET Data Model Code Generation Strategy from None to Default to create a data model based on ObjectContext rather than DbContext</li>
  <li>Turning off pluralisation so the property name is Mno instead of Mnoes</li>
  </ol>
  
  <p>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.</p>
  
  <p>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?</p>