亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更改asp.net core 2.2 IdentityUser的Id類型

更改asp.net core 2.2 IdentityUser的Id類型

C#
慕婉清6462132 2022-06-12 11:00:13
我是 dot-net core 2.x 的新手,所以...我想將 asp.net core 2.2 IdentityUser 中的 Id 類型從 string 更改為 int。我通過谷歌(和 stackoverflow 搜索工具)找到的所有示例都為我提供了 asp.net core 2.0 的示例,當您搭建 Identity(2.2 沒有提供)時,它提供了一個 ApplicationUser。所以,我不知所措..我嘗試的第一件事(我寄予厚望)是:services.AddDefaultIdentity<IdentityUser<int>>()  .AddRoles<IdentityRole>()  .AddDefaultTokenProviders()  .AddEntityFrameworkStores<ApplicationDbContext>();但是,當我嘗試添加遷移 InitialCreate -Context ApplicationDbContext 時出現以下錯誤:訪問“程序”類上的 IWebHost 時出錯。在沒有應用程序服務提供商的情況下繼續。錯誤:GenericArguments[0],“Microsoft.AspNetCore.Identity.IdentityUser`1[System.Int32]”,在“Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole, TUserLogin,TUserToken,TRoleClaim]' 違反了 'TUser' 類型的約束想法?想法?我可以閱讀的文檔?
查看完整描述

2 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

要將 IdentityUser 鍵類型從 string 更改為 int,您還需要將 更改IdentityDbContext為IdentityDbContext<IdentityUser<int>,IdentityRole<int>,int>.


啟動.cs


services.AddDefaultIdentity<IdentityUser<int>>()

    .AddDefaultUI(UIFramework.Bootstrap4)

    .AddEntityFrameworkStores<ApplicationDbContext>();

ApplicationDbContext


public class ApplicationDbContext : IdentityDbContext<IdentityUser<int>,IdentityRole<int>,int>

{

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)

        : base(options)

    {

    }

}

刪除Migrations文件夾并刪除現有數據庫


運行命令添加和更新數據庫


查看完整回答
反對 回復 2022-06-12
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

您可以編寫自己的實現,該實現源自IdentityUser


public class AppUser : IdentityUser<int> 

{


}

然后注冊它:


services.AddDefaultIdentity<AppUser>()

如果您想向用戶模型添加其他屬性,這也很有用。


請記住,因為您正在更改表的主鍵,所以您需要重新創建這些表而不是更新它們。


查看完整回答
反對 回復 2022-06-12
  • 2 回答
  • 0 關注
  • 179 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號