我正试图在Visual studio 2015预览版中创建一个Web应用程序。我正在尝试使用命令行脚手架创建视图。我使用了以下命令:
k gen controller -name ProductController -m ViewModels.Product -dc ProductContext -f -udl
ViewModels.Product
类在另一个项目中定义,并在UI项目中引用。我在运行命令时遇到以下错误:
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.
在Product
类中,我添加了对System.ComponentModel.DataAnnotations
的引用,并定义了具有[key]
属性的属性。它正在创建DataContext类,而不是视图。
任何人都请告诉我哪里错了。
这是目前脚手架的一个已知问题,作为解决方案,您可以在生成的DbContext的OnModelCreating方法中配置Key,如下所示:
builder.Entity<Product>().Key(p => p.MyKeyName);
然后再搭脚手架。