需要从nvarchar的改变Asp.Net标识(RTM)用户ID(128)唯一标识符 [英] Need to change Asp.Net Identity (RTM) User Id from nvarchar(128) to uniqueidentifier

查看:123
本文介绍了需要从nvarchar的改变Asp.Net标识(RTM)用户ID(128)唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Asp.Net 1.0身份(RTM版本)。默认实现创建AspNetUsers表。将ID列类型类型为nvarchar(128)。

Using Asp.Net Identity 1.0 (RTM version). The default implementation creates an AspNetUsers table. The Id column type is nvarchar(128).

在正在创建的数据库和表,我只是想更改用户ID的类型是唯一标识符,而不是为nvarchar(128)。我试图在OnModelCreating覆盖范围内使用这个.HasColumnType(唯一标识符)...但它抛出的错误。

When the database and tables are being created, I would simply like to change the type of the User Id to be uniqueidentifier instead of nvarchar(128). I have attempted this using .HasColumnType("uniqueidentifier") within the OnModelCreating override... but it throws errors.

微软表示,它很容易......但我会倾向于不同意...

Microsoft indicates that it is easy... but I would tend to disagree...

http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity

由于您控制数据库架构,常见的任务,如更改
  表名称或改变主键的数据类型是简单的事情。

Since you control the database schema, common tasks such as changing table names or changing the data type of primary keys is simple to do.

所以,根据自己的极简,完全非技术文档,这似乎是一个共同的任务,改变主键的数据类型...但似乎没有被任何简单的有关情况。请帮助。

So according to their extremely brief and completely non-technical documentation this seems to be a common task, changing the data type of a primary key... but there doesn't seem to be anything simple about it. Please help.

推荐答案

如果您更新到最新的夜间位,你可以尝试新的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);
        }
    }

这篇关于需要从nvarchar的改变Asp.Net标识(RTM)用户ID(128)唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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