在 ASP.NET MVC5 Identity 系统中进一步扩展 ApplicationUser 类 [英] Farther extending ApplicationUser class in ASP.NET MVC5 Identity system

查看:18
本文介绍了在 ASP.NET MVC5 Identity 系统中进一步扩展 ApplicationUser 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为大学创建一个简单的应用程序,学生可以在其中提出某种类型的请求,然后由特定专业的员工处理.

I'm creating a simple application for university where a student can make some type of request which is then processed by an employee of particular speciality.

我想使用默认的 MVC5 身份系统并使用 TPH 模式扩展 ApplicationUser 类.所以我给ApplicationUser添加了通用属性:

I would like to use default MVC5 identity system and to extend the ApplicationUser class using TPH pattern. So I added the common properties to the ApplicationUser:

public class ApplicationUser : IdentityUser
    {
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        [Required]
        public string Email { get; set; }
    }

然后我创建了两个继承 ApplicationUser 的类:

then I created two classes which inherits the 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; }
    }

我目前想要的是让两种类型的用户都注册为基本身份,并将两者保存在一个表中,如 asp.net 上的继承示例

what I currently want is to make both types of users register as a base Identity and to keep both in a single table as in the inheritance example on asp.net

正如我所想,在 AccountController 中初始化用户变量就足够了,然后将其作为学生或员工传递给 UserManager.但在尝试注册为学生后,我收到此异常:

As I thought, it would be enough to initialize user var in AccountController which is then passes to the UserManager as a Student or as an Employee. But after trying to register as a Student i'm getting this exception:

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; }
        ...
    }

和控制器动作的一部分:

and a part of controller's action:

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 设置为抽象类,但没有成功.任何帮助将不胜感激.

I tried setting ApplicationClass to an abstract class, but no luck. Any help would be appreciated.

更新:问题不在于代码本身.在对模型进行这些更改后,我只是没有删除(或更新)数据库.之后一切正常.

UPDATE: The problem wasn't in the code itself. I simply haven't dropped (or updated) the database after making these changes to the model. After it everything works just fine.

推荐答案

@Dragonheart:我试过这个 repro,如果你删除上下文类中的 DBSet 声明它会正常工作.IdentityDbContext 将处理您的 TPH 模式并在表中添加一个鉴别器列以区分子类.

@Dragonheart: I tried this repro and it would work fine if you remove the DBSet declarations in you context class. The IdentityDbContext would handle you TPH pattern and add a Discriminator column in the table to differentiate the child classes.

这篇关于在 ASP.NET MVC5 Identity 系统中进一步扩展 ApplicationUser 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆