zoukankan      html  css  js  c++  java
  • 如何增加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

  • 相关阅读:
    android动态主题切换(RRO 技术)
    Android设计模式-单例模式
    Android 设计模式
    简单理解Binder机制的原理
    Android的Activity启动流程分析
    android 多线程实现方式、并发与同步学习总结
    RecyclerView 缓存机制 | 如何复用表项?
    recyclerview 缓存讲解
    csharp中实现图片拖拽
    特征提取的综合实验(多种角度比较SIFT、SURF、BRISK、ORB、FREAK等局部特诊点检测和描述算法)(2021版)
  • 原文地址:https://www.cnblogs.com/jqdy/p/5946875.html
Copyright © 2011-2022 走看看