我正在使用 Entity Framework 和 MVC3,我的问题是如果模型从另一个类继承,我无法搭建 Controller 。
例子:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class User : Person
{
public string Email { get; set; }
public string Password { get; set; }
}
public class Context : DbContext
{
public DbSet<Person> PersonSet { get; set; }
}
当我尝试添加 Controller 时
User
使用模板Controller with read/write actions and views, using Entity Framework
我收到此错误:'User' is not part of the specified 'Context' class, and the 'Context' class could not be modifed to add a 'DbSet' property to it. (For example, the 'Context' class might be in a compiled assembly.)
我可以添加
public DbSet<User> UserSet { get; set; }
到 Context
但我认为这不是正确的方法。
最佳答案
目前您的 User
不是持久类型,因为上下文不知道该类型,因此您不能使用持久实体类型的脚手架为 User
创建 Controller 类型。如果您想存储 User
及其在数据库中的属性将其添加到您的上下文中:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>();
}
如果不想坚持
User
到数据库,您不能使用脚手架来创建 Controller 。
关于asp.net-mvc-3 - 无法添加从其他类继承的模型的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10769740/