我正在使用Visual Studio 2012,MVC 4和Razor(CSHTML)。 我使用Entity Framework 6自动生成AdventureWorks2012数据库MVC模型。 我创建了一个新文件夹DAL(数据访问层)并在此文件夹中创建了我的DataContext类。
using AdventureWorks_site.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace AdventureWorks_site.DAL
{
public class AdventureWorksContext : DbContext
{
public DbSet<Person> person { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure Code First to ignore PluralizingTableName convention
// If you keep this convention, the generated tables
// will have pluralized names.
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
当我尝试基于Person类和上面的AdventureWorksContext创建一个Controller时,它会显示一条错误消息,指出Person类没有键,因此它失败了。我可以修改类并添加一个键,但是,因为它是一个自动生成的类,所以有可能需要在某个时刻再次生成它,以恢复我所做的任何更改。有没有办法从上下文类中分配一个键?或者有人可以提供不同的方法。