Customizing existing Identity data store and adding new entities

ASP.NET Core Identity model uses Entity Framework Core Code First model and, because of this extension, it is quite simple. We can add new classes that represent tables and provide relationships with existing Identity tables. We can also derive new classes from existing Identity entities and add more properties, such as a LinkedIn profile, Twitter, and so on.

Here, in this section, we will add a table known as User Profile, which contains information related to an employee's designation, and add some properties, such as a Twitter and LinkedIn profile in the Identity user entity itself.

The following is the updated ApplicationUser class, which inherits from IdentityUser and contains new properties such as a Twitter handle, LinkedIn profile, and a Skype account:

    public class ApplicationUser : IdentityUser 
{
public string TwitterHandler { get; set; }
public string LinkedInProfileLink { get; set; }
public string SkypeAccount { get; set; }
}

And here are a few more entities for designation, organization, and user profile:

    public class UserProfile 
{
[Key]
public long UserProfileID { get; set; }
public bool IsActive { get; set; }
public int DesignationID { get; set; }
public Designation Designation { get; set; }
public int OrganizationID { get; set; }
public Organization Organization { get; set; }
public DateTime EffectiveDate { get; set; }
public int ApplicationUserId { get; set; }
public ApplicationUser User { get; set; }
}

public class Designation
{
[Key]
public int DesignationID { get; set;}
public string DesgName { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
}

public class Organization
{
[Key]
public int OrganizationID { get; set; }
public string OrganizationName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string PoBoxNo { get; set; }
public string Website { get; set; }
public bool IsActive { get; set; }
public List<Designation> Designations { get; set; }
}
To learn about Entity Framework Code First model, please refer to this link http://ef.readthedocs.io/en/latest/intro.html.

After creating these entities, we have to add the DbSet entries to create tables. Add this code snippet in ApplicationDbContext, as follows:

    public DbSet<Organization> Organizations { get; set; } 
public DbSet<Designation> Designations { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }

Now we will add the Entity Framework migration, as done before, and that will create another .cs file that contains code to create tables. After running that migration, it will create the following tables:

This is how we can extend the Identity tables and configure Identity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.89