how to create a new user when inheriting from IdentityUser

asp.net-identity c# entity-framework entity-framework-6 linq

Question

I have the following class derived from IdentityUser. Person class is stored in AspNetUsers table in the database, and everything looks good on the database-side.

public class Person : IdentityUser
{
    [Required]
    [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
    public string FirstName { get; set; }

    [Required]
    [StringLength(150, ErrorMessage = "Last name cannot be longer than 150 characters.")]
    public string LastName { get; set; }
}

My questions is about creating a new user (Person) that has firstname and lastname. I guess, first I need to do this:

var user = new IdentityUser() { UserName = "testing" };
IdentityResult result = userManager.Create(user,"123456");

This will insert a new row to AspNetUsers table with null Firstname and Lastname fields. Then, by using LinQ, I need to update Firstname and Lastname fields of the existing record. Is this approach reasonable? Is there any other recommended way of doing this?

1
1
2/23/2015 5:40:13 AM

Accepted Answer

Since Person is an IdentityUser just do the following:

var user = new Person() { UserName = "xx", Firstname = "xy", Lastname = "yy" };
IdentityResult result = userManager.Create(user, "p@ssword");

This should handle everything necessary.

3
2/23/2015 9:48:33 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow