3

Is there a way to create a custom User and Role without specifying the TKey string in IdentityUser, IdentityRole, and IdentityDbContext? I ask because it seems to think I don't want the auto-generated primary key Id anymore and I absolutely do. Doing what I've done below, UserManager.Create(user, password) will fail with an EntityValidationError on Id.

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }
}

public class ApplicationUserLogin : IdentityUserLogin
{
}

public class ApplicationUserClaim : IdentityUserClaim
{
}

public class ApplicationUserRole : IdentityUserRole
{
}

public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }

    [Required]
    public string Description { get; set; }
}


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public MyAppDb()
        : base("MyAppDb")
    {
    }

    public static MyAppDb Create()
    {
        return new MyAppDb();
    }
}
Sean Newcome
  • 1,497
  • 2
  • 17
  • 24

2 Answers2

0

I'm still learning this new ASP.NET Identity system but have you tried omitting generic types like this:

public class ApplicationUser : IdentityUser
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }
}

same goes for ApplicationRole

MyAppDb would look like this:

public class MyAppDb: IdentityDbContext<ApplicationUser>

By default the IDs will be strings with auto-generated GUIDs in the DB

dima
  • 1,181
  • 1
  • 9
  • 20
  • I've created a custom role `ApplicationRole` and you have to specify it in the `MyAppDb: IdentityDbContext<...>`. – Sean Newcome Mar 29 '14 at 00:00
  • So I tried your suggestion, but had to create `MyAppDb` like this: `public class GEOdaisiaMembershipDb : IdentityDbContext` and got this error `The entity type IdentityRole is not part of the model for the current context.` on this line of code `var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);` when registering a user. – Sean Newcome Mar 29 '14 at 00:28
  • take a look at this [thread](http://stackoverflow.com/questions/19865245/how-to-extend-microsoft-aspnet-identity-entityframework-identityrole) – dima Mar 29 '14 at 00:29
  • you need to add it to the context `public DbSet Roles { get; set; }` – dima Mar 29 '14 at 00:31
  • I looked at that post you sent above and it doesn't help. I'm using AspNet.Identity2.0. The `AspNetRoles` table exists. And I've created a controller and view that successfully creates roles. I see them in the database. – Sean Newcome Mar 29 '14 at 00:38
  • So the user was created in the database. So why is `var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);` having an issue? Hmmm. Maybe I've been looking at this stuff too long and my mind is too busy. – Sean Newcome Mar 29 '14 at 00:48
  • not sure, I've started learning ASP.NET Identity not so long ago myself so by trying to help you I'm actually in a way helping myself :) can I see how and where `CreateIdentityAsync` is set up? – dima Mar 29 '14 at 00:54
  • Nope, because it is in the `UserManager`. The UserManager is instantiated like this: `UserManager UserManager = new UserManager(new UserStore(new MyAppDb()));` – Sean Newcome Mar 29 '14 at 01:01
  • It is in the `Microsoft.AspNet.Identity` namespace. – Sean Newcome Mar 29 '14 at 01:07
  • So, I suppose I have to answer this question now that I have Users and Roles both with custom properties on them, and both successfully being created in the database. However, I will open a new question about what is now happening with `UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)`. Thank you @dima you got me started down the right path--which was comment-out all this extra garbage I've been doing and keep it simple. – Sean Newcome Mar 29 '14 at 01:21
  • no problem, sorry I couldn't be more helpful since like I said this stuff is quite new to me too... also your best bet to also post a piece of code where you get the error, i.e. where you call `CreateIdentityAsync` – dima Mar 29 '14 at 02:15
  • You were helpful. BTW, that line of code, is the line of code calling `CreateIdentityAsync()` that is producing the error. ;) – Sean Newcome Mar 29 '14 at 02:30
0

It appears the answer is "NO". If you specify the TKey on User and/or Role the primary keys are no longer created for you.

It seems I was trying to over-complicate things. Thanks @dima for helping me decide to un-complicate things. Here is what I did to successfully get users and roles (both with custom properties) to successfully work, i.e., to successfully create records in the database via a controller and view:

UPDATE: You may want to look at the link I provided at the bottom for a better/simpler solution.

public class ApplicationUser : IdentityUser
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }
}

//public class ApplicationUserLogin : IdentityUserLogin
//{
//}

//public class ApplicationUserClaim : IdentityUserClaim
//{
//}

//public class ApplicationUserRole : IdentityUserRole
//{
//}

public class ApplicationRole : IdentityRole
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }
}


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
    public MyAppDb()
        : base("MyAppDb")
    {
    }
}

However, a new issue has arose with the UserManager. Specifically, I'm getting the error The entity type IdentityRole is not part of the model for the current context. on this line of code var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

UPDATE: Fixed this error here: Why am I getting an IdentityRole error?

Community
  • 1
  • 1
Sean Newcome
  • 1,497
  • 2
  • 17
  • 24