如何增加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:\Users\OleKristian\Documents\Programmering\ShoppingList\src\ShoppingList\Controllers\HomeController.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:\Users\OleKristian\Documents\Programmering\ShoppingList\src\ShoppingList\Controllers\HomeController.cs 20 Active


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM