Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用自定义标识用户(ASP.NET 5.2EF 6和标识2)时,用户角色表上的额外列_C#_Mysql_Entity Framework_Asp.net Mvc 5_Asp.net Identity - Fatal编程技术网

C# 使用自定义标识用户(ASP.NET 5.2EF 6和标识2)时,用户角色表上的额外列

C# 使用自定义标识用户(ASP.NET 5.2EF 6和标识2)时,用户角色表上的额外列,c#,mysql,entity-framework,asp.net-mvc-5,asp.net-identity,C#,Mysql,Entity Framework,Asp.net Mvc 5,Asp.net Identity,我正在为我的MVC应用程序使用基于4层的体系结构:域、数据、服务和MVC。 在我的域层中,我创建了一个从IdentityUser继承的Employee类。它包含自定义属性和GenerateUserIdentityAsync方法,我是从MVC中identitymodes中的应用程序用户类获得的 public class Employee : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentity

我正在为我的MVC应用程序使用基于4层的体系结构:域、数据、服务和MVC。 在我的域层中,我创建了一个从IdentityUser继承的Employee类。它包含自定义属性和GenerateUserIdentityAsync方法,我是从MVC中identitymodes中的应用程序用户类获得的

public class Employee : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<Employee> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
    public String CIN { get; set; }
    public String FirstName { get; set; }
public类员工:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
公共字符串CIN{get;set;}
公共字符串名{get;set;}
在我的数据层中,我创建了一个从IdentityDbContext继承的上下文。我在数据库集中没有提到employee。我还添加了许多关于我的应用程序,特别是employee类的内容

public class MyCWCContexte : IdentityDbContext<Employee>
{
    public MyCWCContexte()
        : base("name=CWCDB")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
公共类mycwcontext:IdentityDbContext
{
公共mycwcontext()
:base(“name=CWCDB”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
我做的最后一个更改是,我更改了StartUp.Auth.cs和IdentityConfig文件,使它们与我的Employee类和上下文相匹配

 public class ApplicationUserManager : UserManager<CWC.Domain.Entities.Employee>
{
    public ApplicationUserManager(IUserStore<CWC.Domain.Entities.Employee> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    {
        var manager = new ApplicationUserManager(new UserStore<CWC.Domain.Entities.Employee>(context.Get<MyCWCContexte>()));
        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<CWC.Domain.Entities.Employee>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
公共类应用程序管理员:用户管理员
{
公共应用程序服务器管理器(IUserStore存储)
:基地(商店)
{
}
公共静态应用程序SerManager创建(IdentityFactoryOptions
UserId和RoleId是表的PKs,Employee_Id是Employee的FK。我认为UserId应该是FK。
这确实是一个问题,因为从usermanager(例如)授权注释和GetRoles不起作用。usermanager.GetRoles(Employee.Id)总是返回null,并且我得到的Employee.Id具有从数据库得到的正确值。
我认为这是因为比较是在员工id而不是用户id上进行的。

你对此有什么想法吗?我是ASP.NET的初学者。提前谢谢你。

通过从DbContext:IdentityDbContext中删除泛型(Employee)来更正它 我现在的背景是:
公共类mycwccontext:IdentityDbContext通过从DbContext:IdentityDbContext中删除泛型(Employee)对其进行了更正 我现在的背景是:
公共类MyCWCContext:IdentityDbContext

您是否也创建了自己的角色类?您是否也创建了自己的角色类?这是问题的答案还是补充?@GertArnold an answer这是问题的答案还是补充?@GertArnold an answer