简体   繁体   English

ASP.NET Core 2:UserManager与DbContext

[英]ASP.NET Core 2: UserManager vs. DbContext

I'm using the generic IdentityUser<T> class for my user model and with using string as primary key everything works fine, but I would like to use long as key type. 我正在为我的用户模型使用通用的IdentityUser<T>类,并且使用string作为主键,一切正常,但是我想使用long作为键类型。 So, I'm using the generic version of IdentityUser. 因此,我使用的是IdentityUser的通用版本。 Now I discovered that the UserManager has the following definition: 现在,我发现UserManager具有以下定义:

public class UserManager<TUser> : IDisposable where TUser : class

and the following function 和以下功能

public virtual Task<TUser> FindByIdAsync(string userId);

It seems that this doesn't fit together because the function requires a string as key/ID. 似乎这并不适合,因为该函数需要使用字符串作为键/ ID。 TUser must be TUser<TKey> . TUser必须是TUser<TKey>

The question is now if there is any performance loss because of parsing string to long (of course there is) or is it better to get the user object direct from DbContext with a normal database select? 现在的问题是,是否由于将字符串解析为long(当然有)而导致性能下降,还是通过普通数据库选择直接从DbContext获取用户对象更好? Does the UserManager has any benefits so that it would be recommended to use it even with the string key requirement? UserManager是否有任何好处,以便即使有字符串键要求也建议使用它?

Thanks! 谢谢!

You can setup your User table to use a different type for a primary key by extending the IdentityUser class like so: 您可以通过扩展IdentityUser类来将User表设置为对主键使用其他类型:

public class MyUser : IdentityUser<long>

And then injecting UserManager<MyUser> into your classes to manage them. 然后将UserManager<MyUser>注入您的类中以对其进行管理。

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-primary-key-configuration?tabs=aspnetcore2x https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-primary-key-configuration?tabs=aspnetcore2x

FindByIdAsync always takes string because it is a common denominator of every class. FindByIdAsync始终采用字符串,因为它是每个类的公分母。 Instead of polluting with generics all over the place (like in Identity v2) you will have to convert your digits to string and pass it on. 而不是到处都是泛型的污染(例如在Identity v2中),您将不得不将数字转换为字符串并继续传递。 On DB side it is handled by EF anyway for you. 在数据库方面,EF仍会为您处理它。

You can have your own UserManager class that inherits from UserManager and have a method override FindByIdAsync(long id) that just does id.ToString() 您可以拥有自己的从UserManager继承的UserManager类,并且可以使用只重写FindByIdAsync(long id)的方法覆盖FindByIdAsync(long id) id.ToString()

    public virtual Task<TUser> FindByIdAsync(long userId)
    {
         return base.FindByIdAsync(userId.ToString());
    }

From this link you can see that it is possible to change type of the key. 这个链接,你可以看到,它是可以改变的主要类型。 Initially it is set to string, so that you can use values like int or guid out of the box. 最初将其设置为字符串,以便您可以直接使用int或guid之类的值。

Code taken from mentioned link. 代码来自提到的链接。 Firstly change your user to desired key type: 首先将您的用户更改为所需的密钥类型:

 public class ApplicationUser : IdentityUser<long>
 {
 }

Edit Application Role class: 编辑应用程序角色类:

public class ApplicationRole : IdentityRole<long>
{
} 

In application context class, add your new key: 在应用程序上下文类中,添加新密钥:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM