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

查看:161
本文介绍了在ASP.NET MVC5识别系统进一步扩展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; }
    }

我目前要的是让这两种类型的用户登记为基础的身份,并保持双方在一个表中的<一个href=\"http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application\">inheritance例如在asp.net

因为我认为,这将是足以初始化的AccountController用户VAR然后将其传递给的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设置为一个抽象类,但没有运气。任何帮助将是AP preciated。

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

更新:这个问题是不是在code本身。我根本就没有做这些改变模型后回落(或更新)数据库。后一切正常就好了。

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:我想这个摄制,如果你删除你的上下文类的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识别系统进一步扩展ApplicationUser类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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