I’m using Entity Framework and MVC3, and my problem is that I can’t scaffold Controllers if the Model inherits from another Class.
Example:
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; }
}
When I try to Add the Controller User using template Controller with read/write actions and views, using Entity Framework I get this error:
‘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.)
I could add public DbSet<User> UserSet { get; set; } to the Context but I don’t think it is the right aproach.
At the moment your
Useris not persisted type because context doesn’t know that type so you cannot use scaffolding of persisted entity types to create controller forUsertype. If you want to storeUserand its properties in database add this to your context:If you don’t want to persist
Userto database you cannot use scaffolding for controller creation.