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

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

问题描述

(ASP.NET MVC 5,EF6,VS2013)

(ASP.NET MVC 5, EF6, VS2013)

我想弄清楚如何为从字符串改变ID字段的类型为int 的类型:

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:

(从大会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身份框架,要求用户id是一个字符串的方法。我会需要重新实现这些类呢?

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?

为什么我不想存储IDS的GUID在user表中的解释:

-there将是通过外键关联的数据到用户表其他表。 (当用户保存在站点上的内容。)我认为没有理由使用更大的字段类型,并花额外的数据库空间没有明显的优势。 (我知道有关于使用GUID的VS INT IDS其他职位,但似乎许多建议,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小组决定实施标识这种方式?难道我正在试图改变这一个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.)

谢谢...

-Ben

推荐答案

所以,如果你想诠释的ID,你需要创建自己的POCO IUSER类并实现你的IUserStore在1.0 RTM版本定制IUSER类。

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本易(IER)现在。希望的东西将在每晚构建很快。

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例如: How让夜间竣工图

如果您更新到最新的夜间位,你可以尝试新的1.1 ALPHA1的API应该让现在这个更简单:下面是GUID的字符串,而不是堵什么应该像例如:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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