内容简介:我正在为大学创建一个简单的应用程序,学生可以提出某种类型的请求,然后由特定专业的员工处理.我想使用默认的MVC5身份系统,并使用TPH模式扩展ApplicationUser类.所以我将常用属性添加到ApplicationUser:然后我创建了两个继承ApplicationUser的类:
我正在为大学创建一个简单的应用程序,学生可以提出某种类型的请求,然后由特定专业的员工处理.
我想使用默认的MVC5身份系统,并使用TPH模式扩展ApplicationUser类.所以我将常用属性添加到ApplicationUser:
public class ApplicationUser : IdentityUser
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Email { get; set; }
}
然后我创建了两个继承ApplicationUser的类:
public class Student : ApplicationUser
{
public string PersonalNumber { get; set; }
public bool Monitor { get; set; }
public virtual Group Group { get; set; }
public virtual ICollection<Request> Requests { get; set; }
}
public class Employee : ApplicationUser
{
public virtual EmployeeSpeciality EmployeeSpeciality { get; set; }
public virtual ICollection<Request> Requests { get; set; }
}
我目前想要的是让两种类型的用户注册为基本身份,并将它们保存在单个表中,如 inheritance example on asp.net 中所示
正如我所想,在AccountController中初始化用户var就足够了,然后将其作为学生或员工传递给UserManager.但在尝试注册成为学生后,我得到了这个例外:
Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'PersonalNumber'. Invalid column name 'Monitor'. Invalid column name 'EmployeeSpeciality_Id'. Invalid column name 'Group_Id'.
我的上下文类:
public class EntityContext : IdentityDbContext
{
public EntityContext()
: base("DbConnection")
{
}
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<Student> Students { get; set; }
public DbSet<Employee> Employees { get; set; }
...
}
和控制器动作的一部分:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new Student()
{
UserName = model.UserName,
FirstName = model.FirstName,
LastName = model.LastName,
Email = model.Email
};
var result = await UserManager.CreateAsync(user, model.Password);
我尝试将ApplicationClass设置为抽象类,但没有运气.任何帮助,将不胜感激.
更新:问题不在代码本身.在对模型进行这些更改后,我根本没有删除(或更新)数据库.一切都运行得很好.
以上所述就是小编给大家介绍的《entity-framework – 进一步扩展ASP.NET MVC5身份系统中的ApplicationUser类》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Beginning Google Maps API 3
Gabriel Svennerberg / Apress / 2010-07-27 / $39.99
This book is about the next generation of the Google Maps API. It will provide the reader with the skills and knowledge necessary to incorporate Google Maps v3 on web pages in both desktop and mobile ......一起来看看 《Beginning Google Maps API 3》 这本书的介绍吧!