1

I have implemented the MVC 5 Indentity provider as my security module and cannot link the tables to my existing database since I am using int primary keys and the Asp tables are using GUIDs. Does anyone know whether I can change the Asp tables to use int instead? I have tried finding some information on the topic with a Google search, but could not find something of value. I am going to keep trying and will provide details on the subject when I have more information. I was just hoping that someone could save me some time by pointing me in a direction that could provide some insight.

More Information:

My project makes use of MVC 5 as far as possible with some WebApi instead of controllers. I communicate with the MSSQL database using Entity Framework 6.

WJK
  • 683
  • 2
  • 6
  • 20
  • I found this post: http://stackoverflow.com/questions/21383901/asp-net-identity-update-column-type-for-primary-key-id-in-aspnetusers-table-from?s=1320f1f4-6629-4397-869e-daaefee0d948#new-answer which provides more information on the subject. – WJK Mar 03 '14 at 08:50
  • Okay, there may be a space saving here, but in general, if you care about the *value* of auto-generated keys, you're using them wrong. It shouldn't matter how the system or the database generates the IDs, provided that they're unique. – Damien_The_Unbeliever Mar 03 '14 at 11:03
  • The problem is more along the line of uniformity, as my database is already designed to use integer as the primary key. In other words, all my existing tables has integer keys, but now the asp tables has GUIDs – WJK Mar 03 '14 at 15:08
  • It seems shortly after this question was posted, ASP.NET Identity version 2.0.0 was released (on March 20, 2014) which now natively supports extending/changing the ID/primary key's type. See http://blogs.msdn.com/b/webdev/archive/2014/03/20/test-announcing-rtm-of-asp-net-identity-2-0-0.aspx for the announcement and more details – Funka May 19 '14 at 18:48
  • Thank you for the link, Funka! I was not aware of that. I will definitely check it out. If you can post it as an answer, I will mark is as such. – WJK May 21 '14 at 05:45

1 Answers1

1

have a look at this answer by hao-kung in this post

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.

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.

Updated with 1.1-alpha1 example: How to get nightly builts

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);
    }
}
Community
  • 1
  • 1
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
  • Thank you for the link! It is very helpful. The article I linked to in my comment also points to this article. – WJK Mar 03 '14 at 15:11