• 如何增加Asp.Net Core生成的模板网站中用户信息表中的列(AspNetUsers)


    环境:

    1.VS2015 Community 14.0.25431.01 Update 3;

    2.其他环境(具体哪一个影响不太清楚,都列在这儿)

    使用的系统模板

    利用系统提供的模板,并选择个人身份验证。如图:

    问题:

    模板提供的身份认证数据库中的AspNetUsers表,需要根据需要增加列。以下图为例,绿色框中的列都是模板默认的,我要增加一列(以Test为例)。

    解决过程:

            刚刚接触MVC、EF的概念,不知道如何操作。查阅了大量资料后发现,网上大部分类似内容都是基于mvc3的,最后还是在stackoverflow上找到一篇(中英文附后)。

            但是一步一步按图索骥做下来,在AspNetUsers表中就是加不上Test列,其中还遇到一些错误,例如:几个地方显示使用的方法错误。

            最后发现,该例子是基于asp.net 5/6的,许多需要添加的内容模板中已经有了,考虑是不是asp.net core有一些变化。

            于是,直接将Test属性直接添加到ApplicationUser中去,发现问题解决了。

            原来与之前考虑的一样,asp.net core已经将所需的所有内容都已经考虑好了,不需要做其他的修改。还有,这个版本与之前5/6版本几个方法发生了变化,所以出现了找不到方法的错误(已经标注到附录例子中了)。

    我的具体代码如下:(Models/ApplicationUser.cs)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    
    namespace Test.Models
    {
        // Add profile data for application users by adding properties to the ApplicationUser class
        public class ApplicationUser : IdentityUser
        {
            public string Test { get; set; }//之前该行是空白
        }
    }

     附:stackoverflow的例子 

    Accssing ASP.NET 5 / MVC6 Identity Custom Profile data properties

    ASP.NET 5 / mvc6身份自定义配置文件数据属性

    I have made a sample web app named ShoppingList using the asp.net 5 web application template (Mvc6/MVC core/Asp.net-5). I wanted to extend the user profile with a custom field name DefaultListId.

    我做了一个简单的网页应用,该应用使用asp.net 5 网络应用模板(Mvc6/MVC Core/Asp.net 5)。我想扩展用户配置文件,增加客户名称字段(DefaultListID)。

    The ApplicationUser.cs:

    namespace ShoppingList.Models
    {
        // Add profile data for application users by adding properties to the ApplicationUser class
        public class ApplicationUser : IdentityUser
        {
            public int DefaultListId { get; set; }
        }
    }

    In the home controller I would like to access the data stored for this property. I tried:

     在home控制器中,我想使用该属性,我做了如下尝试:

    namespace ShoppingList.Controllers
    {
        public class HomeController : Controller
        {
           private UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
    
            public IActionResult Index()
            {
               var userId = User.GetUserId();
               ApplicationUser user = userManager.FindById(userId);
    
                ViewBag.UserId = userId;
                ViewBag.DefaultListId = user.DefaultListId;
    
                return View();
            }
        //other actions omitted for brevity

    However I get the following errors:

    但是,我得到错误信息如下:

    Severity Code Description Project File Line Suppression State Error CS7036 There is no argument given that corresponds to the required formal parameter 'optionsAccessor' of 'UserManager.UserManager(IUserStore, IOptions, IPasswordHasher, IEnumerable>, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, IServiceProvider, ILogger>, IHttpContextAccessor)' ShoppingList.DNX 4.5.1, ShoppingList.DNX Core 5.0 C:UsersOleKristianDocumentsProgrammeringShoppingListsrcShoppingListControllersHomeController.cs 15 Active

    And...

    还有...

    Severity Code Description Project File Line Suppression State Error CS1061 'UserManager' does not contain a definition for 'FindById' and no extension method 'FindById' accepting a first argument of type 'UserManager' could be found (are you missing a using directive or an assembly reference?) ShoppingList.DNX 4.5.1, ShoppingList.DNX Core 5.0 C:UsersOleKristianDocumentsProgrammeringShoppingListsrcShoppingListControllersHomeController.cs 20 Active

  • 相关阅读:
    php实现导出excel功能(转)
    nginx + php-fpm 开启 PATH_INFO 模式
    openEuler安装mariadb后进程查看有[ERROR] mysqld: Server GSSAPI错误
    CentOS 8开放防火墙端口
    ssh连接PVE下debian11的LXC容器非常慢之解决方法
    debian11安装SRS教程
    用python下载网页或图片
    解决Debian vim鼠标无法选中、复制问题
    在Ubuntu系统中安装pymssql
    Mariadb设置允许远程链接
  • 原文地址:https://www.cnblogs.com/jqdy/p/5946875.html
Copyright © 2020-2023  润新知