gpt4 book ai didi

entity-framework - 在 ASP.NET MVC5 Identity 系统中进一步扩展 ApplicationUser 类

转载 作者:行者123 更新时间:2023-12-04 08:52:28 24 4
gpt4 key购买 nike

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

我想使用默认的 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 中初始化用户变量就足够了,然后将其作为学生或员工传递给 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; }
...
}

和 Controller 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 设置为抽象类,但没有运气。任何帮助,将不胜感激。

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

最佳答案

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

关于entity-framework - 在 ASP.NET MVC5 Identity 系统中进一步扩展 ApplicationUser 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21062981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com