加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

身份更改GUID为int

发布时间:2020-12-15 23:32:20 所属栏目:asp.Net 来源:网络整理
导读:如何将AspNetUser表的PK列从guid更改为int数据类型? 现在可以使用今天发布的最新的asp.net身份版本. 但是我找不到任何地方怎么办? 解决方法 默认情况下,ASP.NET身份(使用实体框架)使用字符串作为主键,而不是GUID,但它会将GUID存储在这些字符串中. 您需要定
如何将AspNetUser表的PK列从guid更改为int数据类型?
现在可以使用今天发布的最新的asp.net身份版本.

但是我找不到任何地方怎么办?

解决方法

默认情况下,ASP.NET身份(使用实体框架)使用字符串作为主键,而不是GUID,但它会将GUID存储在这些字符串中.

您需要定义更多的类,我刚刚创建了一个新项目(我正在使用VS2013 Update 2 CTP),以下是需要更改的身份模型:

public class ApplicationUser : IdentityUser<int,ApplicationUserLogin,ApplicationUserRole,ApplicationUserClaim>
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationUserRole : IdentityUserRole<int>
{
}

public class ApplicationUserLogin : IdentityUserLogin<int>
{
}

public class ApplicationUserClaim : IdentityUserClaim<int>
{
}

public class ApplicationRole : IdentityRole<int,ApplicationUserRole>
{
}

public class ApplicatonUserStore :
    UserStore<ApplicationUser,ApplicationRole,int,ApplicationUserClaim>
{
    public ApplicatonUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

public class ApplicationDbContext
    : IdentityDbContext<ApplicationUser,ApplicationUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

您还需要更新其他几个地方,只需按照编译错误,最常见的更改将是将从User.Identity.GetUserId()返回的字符串转换为整数.

另外,在回答another question(我没有问过这个问题的时候),我提供了一个例子,解决这个问题,看看下面的资料库:

https://github.com/JSkimming/AspNet.Identity.EntityFramework.Multitenant

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读