Search code examples
c#asp.net-coreasp.net-mvc-scaffoldingvisual-studio-2015

Command line scaffolding in asp.Net VNext


I am trying to create a Web Application in Visual studio 2015 Preview. I am trying to create views using the Command Line Scaffolding. I used the following command:

k gen controller -name ProductController -m ViewModels.Product -dc ProductContext -f -udl

The ViewModels.Product class is defined in another project and referenced in UI project. I get the following error while running the command:

Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to figure out the EntityFramework metadata for the model and DbContext.
The entity type 'ViewModels.Product' requires a key to be defined.

In the Product class I have added a reference to System.ComponentModel.DataAnnotations and defined a property with the [key] attribute. It is creating the DataContext class, not views.

Anyone please suggest where I am wrong.


Solution

  • This is a known issue with the scaffolding right now, as a work around, you can configure the Key in the generated DbContext's OnModelCreating method like this:

    builder.Entity<Product>().Key(p => p.MyKeyName);
    

    and then scaffold again.