Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Entity Framework 6.1 (prerelease) IdentityUser with integer ID

In the new Entity Framework 6.1 beta/pre-release they have included the option to change the datatype of the Id column that is used within the database. I am looking into how this should be implemented and have come to the following:

ApplicationUser.cs

public class ApplicationUser : IdentityUser<int, IdentityUserLogin<int>,
IdentityUserRole<int>, IdentityUserClaim<int>>, IObjectState
{
}

AuthenticationContext.cs

public class AuthenticationContext : IdentityDbContext<ApplicationUser, IdentityRole<int, IdentityUserRole<int>>, int, IdentityUserLogin<int>, IdentityUserRole<int>, IdentityUserClaim<int>>
{
    public AuthenticationContext()
        : base("ConnectionString")
    {
    }
}

AccountController.cs

public AccountController() : this(new UserManager<ApplicationUser, int>(new UserStore<ApplicationUser, IdentityRole<int, IdentityUserRole<int>>, int, IdentityUserLogin<int>, IdentityUserRole<int>, IdentityUserClaim<int>>(new AuthenticationContext())))
{
}

public AccountController(UserManager<ApplicationUser, int> userManager)
{
    UserManager = userManager;
}

public UserManager<ApplicationUser, int> UserManager { get; private set; }

When running the application (which is the default EF/MVC app) I get the following errors when clicking Register:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code Additional information: The type 'Microsoft.AspNet.Identity.EntityFramework.IdentityRole2[System.Int32,Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole1[System.Int32]]' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject.`

Obviously I'm doing something wrong, but I'm unsure what I should adjust to make it work. Any suggestions?

like image 876
Steven Thewissen Avatar asked Feb 21 '14 23:02

Steven Thewissen


1 Answers

I would try following this example: http://blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0-alpha1.aspx under "Make the type of Primary Key be extensible for Users and Roles". The difference is that you create separate classes for each identity model.

like image 57
James Reategui Avatar answered Sep 29 '22 14:09

James Reategui