如何更改 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 中的 id 类型 [英] How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUser

查看:23
本文介绍了如何更改 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 中的 id 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(ASP.NET MVC 5、EF6、VS2013)

(ASP.NET MVC 5, EF6, VS2013)

我想弄清楚如何在类型中将Id"字段的类型从字符串更改为整数:

I'm trying to figure out how to change the type of the "Id" field from string to int in the type:

Microsoft.AspNet.Identity.EntityFramework.IdentityUser

为了让新用户帐户与整数 ID 而不是 GUID 相关联.但这似乎比在我的派生用户类中简单地添加一个 int 类型的新 Id 属性要复杂得多.看看这个方法签名:

in order to have new user accounts be associated with an integer ID rather than a GUID. But it seems like this will be more complicated than simply adding a new Id property with type int in my derived user class. Take a look at this method signature:

(来自 Assembly Microsoft.AspNet.Identity.Core.dll)

(from Assembly Microsoft.AspNet.Identity.Core.dll)

public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
  {
  ...
  public virtual Task<IdentityResult> AddLoginAsync(string userId, UserLoginInfo login);
  ...
  }

因此,ASP.NET 标识框架中似乎还有其他方法需要 userId 为字符串.我还需要重新实现这些类吗?

So it seems that there are other methods baked into the ASP.NET identity framework which require the userId to be a string. Would I need to reimplement these classes as well?

解释为什么我不想在用户表中存储 ID 的 GUID:

-将有其他表通过外键将数据与用户表相关联.(当用户在网站上保存内容时.)我认为没有理由使用更大的字段类型并花费额外的数据库空间而没有明显的优势.(我知道还有其他关于使用 GUID 与 int id 的帖子,但似乎很多人都认为 int id 速度更快,占用的空间更少,这仍然让我想知道.)

-There will be other tables that relate data to the users table via a foreign key. (When users save content on the site.) I see no reason to use the larger field type and spend extra database space with no clear advantages. (I know that there are other posts about using GUIDs vs int ids, but it seems like many suggest that int ids are faster and use less space, which still leaves me wondering.)

-我计划公开一个宁静的端点,以允许用户检索有关特定用户的数据.我认为:

-I plan to expose a restful endpoint to allow users to retrieve data about a particular user. I think:

/users/123/name

/users/{af54c891-69ba-4ddf-8cb6-00d368e58d77}/name

有谁知道为什么 ASP.NET 团队决定以这种方式实现 ID?我是否在尝试将其更改为 int 类型时目光短浅?(也许我缺少一些好处.)

Does anyone know why the ASP.NET team decided to implement IDs this way? Am I being short sighted in trying to change this to an int type? (Perhaps there are benefits I'm missing.)

谢谢...

-本

推荐答案

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

So if you want int ids, you need to create your own POCO IUser class and implement your IUserStore for your custom IUser class in the 1.0 RTM release.

>

这是我们没有时间支持的东西,但我现在正在考虑在 1.1 中使它变得更容易(更).希望很快就会在每晚构建中提供一些东西.

This is something we didn't have time to support, but I'm looking into making this easy(ier) in 1.1 right now. Hopefully something will be available in the nightly builds soon.

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

如果您更新到最新的夜间位,您可以尝试新的 1.1-alpha1 apis,这现在应该会更容易:以下是插入 Guids 而不是字符串的样子

If you update to the latest nightly bits, you can try out the new 1.1-alpha1 apis which should make this easier now: Here's what plugging in Guids instead of strings should look like for example

    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);
        }
    }

这篇关于如何更改 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 中的 id 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆