首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >标识将GUID更改为int

标识将GUID更改为int
EN

Stack Overflow用户
提问于 2014-03-21 08:07:34
回答 3查看 12.3K关注 0票数 16

如何将AspNetUser表的PK列从guid数据类型更改为int数据类型?在今天发布的最新asp.net-identity版本中,这应该是可能的。

但是我找不到这是怎么做到的?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-22 00:05:37

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

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

代码语言:javascript
复制
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, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicatonUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

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

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

此外,在回答another question时(我确实问了这个问题),我提供了一个示例解决方案来实现这一点,请参阅下面的存储库:

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

票数 16
EN

Stack Overflow用户

发布于 2016-02-20 17:04:24

如果有人在查找identity 3.0指南时发现了这一点:

代码语言:javascript
复制
public class ApplicationUser : IdentityUser<int>
{
}

public class ApplicationRole : IdentityRole<int>
{
}

public class ApplicationDbContext: 
    IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
}

在Startup.cs文件中,ConfigureServices方法:

代码语言:javascript
复制
services.AddIdentity<ApplicationUser, ApplicationRole>()
    .AddEntityFrameworkStores<ApplicationDbContext, int>()//, int being the only difference
    .AddDefaultTokenProviders();

这就是全部,不需要像在2.0中那样创建管理器

this blog post里找到了这个。

票数 5
EN

Stack Overflow用户

发布于 2014-03-21 12:11:23

请看浩空在这篇post中的回答

因此,如果您需要int,您需要创建自己的POCO IUser类,并在1.0RTM版本中为您的自定义IUser类实现IUserStore。

这是我们没有时间支持的东西,但我现在正在考虑在1.1中使这一点变得更容易。希望很快就能在夜间构建中有一些东西可用。

更新1.1-alpha1示例:如何获取夜间构建

如果您更新到最新版本,您可以尝试新的1.1-alpha1API,这将使这件事变得更容易:例如,插入Guid而不是字符串应该是什么样子

代码语言:javascript
复制
public class GuidRole : IdentityRole<Guid, GuidUserRole> { 
    public GuidRole() {
        Id = Guid.NewGuid();
    }
    public GuidRole(string name) : this() { Name = name; }
}
public class GuidUserRole : IdentityUserRole<Guid> { }
public class GuidUserClaim : IdentityUserClaim<Guid> { }
public class GuidUserLogin : IdentityUserLogin<Guid> { }

public class GuidUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> {
    public GuidUser() {
        Id = Guid.NewGuid();
    }
    public GuidUser(string name) : this() { UserName = name; }
}

private class GuidUserContext : IdentityDbContext<GuidUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> { }
private class GuidUserStore : UserStore<GuidUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> {
    public GuidUserStore(DbContext context)
        : base(context) {
    }
}
private class GuidRoleStore : RoleStore<GuidRole, Guid, GuidUserRole> {
    public GuidRoleStore(DbContext context)
        : base(context) {
    }
}

[TestMethod]
public async Task CustomUserGuidKeyTest() {
    var manager = new UserManager<GuidUser, Guid>(new GuidUserStore(new GuidUserContext()));
    GuidUser[] users = {
        new GuidUser() { UserName = "test" },
        new GuidUser() { UserName = "test1" }, 
        new GuidUser() { UserName = "test2" },
        new GuidUser() { UserName = "test3" }
        };
    foreach (var user in users) {
        UnitTestHelper.IsSuccess(await manager.CreateAsync(user));
    }
    foreach (var user in users) {
        var u = await manager.FindByIdAsync(user.Id);
        Assert.IsNotNull(u);
        Assert.AreEqual(u.UserName, user.UserName);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22547712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档