热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

扩展Roles表后,实体类型IdentityRole不是当前上下文的模型的一部分

如何解决《扩展Roles表后,实体类型IdentityRole不是当前上下文的模型的一部分》经验,为你挑选了1个好方法。

我将由实体框架创建的AspNetRoles扩展为如下所示:

public class AspNetRoles:IdentityRole
{
        public AspNetRoles() : base() { }
        public String Label { get; set; }
        public String ApplicationId { get; set; }
        public AspNetApplications Application { get; set; }

        public static readonly String SystemAdministrator = "SystemAdministrator";
}

我知道,因为我已经扩展了identityrole表,所以必须对usermanager进行更改。这是我所做的:

public class ApplicationUserManager : UserManager
{
    public ApplicationUserManager(IUserStore store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context) 
    {
        var manager = new ApplicationUserManager(new UserStore(context.Get()));
        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator(manager)
        {
            AllowOnlyAlphanumericUserNames= false,
            RequireUniqueEmail = true
        };

        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNOnLetterOrDigit= true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        // Configure user lockout defaults
        manager.UserLockoutEnabledByDefault = true;
        manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        manager.MaxFailedAccessAttemptsBeforeLockout = 5;

        // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
        // You can write your own provider and plug it in here.
        manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider
        {
            MessageFormat = "Your security code is {0}"
        });
        manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider
        {
            Subject = "Security Code",
            BodyFormat = "Your security code is {0}"
        });
        manager.EmailService = new EmailService();
        manager.SmsService = new SmsService();
        var dataProtectiOnProvider= options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider = 
                new DataProtectorTokenProvider(dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
    }
}

// Configure the application sign-in manager which is used in this application.
public class ApplicationSignInManager : SignInManager
{
    public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
        : base(userManager, authenticationManager)
    {
    }

    public override Task CreateUserIdentityAsync(ApplicationUser user)
    {
        return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
    }

    public static ApplicationSignInManager Create(IdentityFactoryOptions options, IOwinContext context)
    {
        return new ApplicationSignInManager(context.GetUserManager(), context.Authentication);
    }
}

public class ApplicationRoleManager : RoleManager, IDisposable
{
    public ApplicationRoleManager(RoleStore store) : base(store)
    { }


    public static ApplicationRoleManager Create(IdentityFactoryOptions options, IOwinContext context)
    {
        //AppIdentityDbContext db = context.Get();
        //AppRoleManager manager = new AppRoleManager(new RoleStore(db));
        return new ApplicationRoleManager(new RoleStore(context.Get()));

        //return manager;
    }
}

public class ApplicationUserStore : UserStore, IUserStore, IUserStore, IDisposable where TUser : IdentityUser
{
    public ApplicationUserStore(DbContext context) : base(context) { }
}

这是我的DBContext:

public class ApplicationDbContext : IdentityDbContext
    {
        public virtual DbSet AspNetUsersExtendedDetails { get; set; }
        public virtual DbSet AspNetApplications { get; set; }
        public virtual DbSet AspNetEventLogs { get; set; }
        public ApplicationDbContext() : base("AppStudio")
        {

        }
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

但是,在启动应用程序时出现此错误:

实体类型IdentityRole不是当前上下文模型的一部分。

我不确定为什么会这样。扩展角色表后,我是否错过了需要更改的内容?



1> Reza Aghaei..:

简短答案

上面代码中的主要问题在于的Create方法UserManager。在该方法中,您应该创建一个UserManager使用,UserStore它可以识别您创建的新角色类。为此,您可以使用拥有的ApplicationUserStore类的实例,或通过以下方式创建新的用户存储:

new UserStore())

如何向IdentityRole添加自定义属性?

要将新属性添加到IdentityRole,您可以按照以下步骤操作:

    创建一个ASP.NET Web应用程序

    确保选择“ MVC”,并且“ 身份验证”是“ 个人用户帐户”

    转到模型文件夹?打开IdentityModels.cs并创建包含要添加的自定义属性的ApplicationRole类:

    public class ApplicationRole : IdentityRole   //My custom role class
    {
        public string ApplicationId { get; set; } //My custom property
    }
    

    更改类型为的参数的接受GenerateUserIdentityAsync方法:ApplicationUserUserManager

    public class ApplicationUser : IdentityUser
    {
        public async Task GenerateUserIdentityAsync(UserManager manager)
        {
    

    更改ApplicationDbContext基类并引入所有通用参数:

    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
    

    生成项目。

    转到“ 工具”菜单?Nuget软件包管理器?单击程序包管理器控制台

    键入Enable-Migrations并按Enter,直到任务完成。

    键入Add-Migration "ApplicationRole"并按Enter,直到任务完成。

    键入Update-Database并按Enter,直到任务完成。

    转到App_Start文件夹?打开IdentityConfig.cs并更改ApplicationUserManager要从UserManager其派生的类 ,还更改其Create方法以返回以下UserManage信息ApplicationRole

    public class ApplicationUserManager : UserManager
    {
        public ApplicationUserManager(IUserStore store)
            : base(store)
        {
        }
    
        public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore(context.Get()));
    

    要管理角色,请ApplicationRoleManager在同一文件中创建类:

    public class ApplicationRoleManager : RoleManager
    {
        public ApplicationRoleManager(IRoleStore store) : base(store) { }
    
        public static ApplicationRoleManager Create(
            IdentityFactoryOptions options,
            IOwinContext context)
        {
            return new ApplicationRoleManager(new RoleStore(context.Get()));
        }
    }
    

    转到App_Start文件夹?打开Startup.Auth.cs并将以下代码添加到ConfigureAuth方法中:

    ConfigureAuthapp.CreatePerOwinContext(ApplicationRoleManager.Create);
    

现在该项目已准备好利用新项目ApplicationRole


推荐阅读
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • Gitlab接入公司内部单点登录的安装和配置教程
    本文介绍了如何将公司内部的Gitlab系统接入单点登录服务,并提供了安装和配置的详细教程。通过使用oauth2协议,将原有的各子系统的独立登录统一迁移至单点登录。文章包括Gitlab的安装环境、版本号、编辑配置文件的步骤,并解决了在迁移过程中可能遇到的问题。 ... [详细]
  • 本文介绍了如何在Azure应用服务实例上获取.NetCore 3.0+的支持。作者分享了自己在将代码升级为使用.NET Core 3.0时遇到的问题,并提供了解决方法。文章还介绍了在部署过程中使用Kudu构建的方法,并指出了可能出现的错误。此外,还介绍了开发者应用服务计划和免费产品应用服务计划在不同地区的运行情况。最后,文章指出了当前的.NET SDK不支持目标为.NET Core 3.0的问题,并提供了解决方案。 ... [详细]
  • 本文介绍了解决java开源项目apache commons email简单使用报错的方法,包括使用正确的JAR包和正确的代码配置,以及相关参数的设置。详细介绍了如何使用apache commons email发送邮件。 ... [详细]
  • [echarts] 同指标对比柱状图相关的知识介绍及应用示例
    本文由编程笔记小编为大家整理,主要介绍了echarts同指标对比柱状图相关的知识,包括对比课程通过率最高的8个课程和最低的8个课程以及全校的平均通过率。文章提供了一个应用示例,展示了如何使用echarts制作同指标对比柱状图,并对代码进行了详细解释和说明。该示例可以帮助读者更好地理解和应用echarts。 ... [详细]
author-avatar
天呀你呀_778
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有