asp.net-mvc-3 - 无法添加从其他类继承的模型的 Controller

标签 asp.net-mvc-3 entity-framework inheritance

我正在使用 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/

相关文章:

c# - Entity Framework : multiple queries vs inner joins

asp.net-mvc-3 - 限制对 View 的访问仅用于调试

.net - 从 Orchard 内的主题渲染图像

Ajax.ActionLink 没有 href 属性以获得更好的 SEO

c# - .NET Core 的 NUnit Entity Framework 集成测试依赖注入(inject)问题

linq - 使用复杂的映射将表达式树从一种类型转换为另一种类型

javascript - 如何访问原型(prototype)链中被自身属性遮蔽的属性?

javascript - 多重继承和多次调用父构造函数

java - 方法链 + 继承不能很好地结合在一起?

c# - Entity Framework 迁移 - 启用 AutoMigrations 以及添加的迁移