This post is a continuation in a series about building a full stack web application. Previous posts in the series:
There were a couple of goals I wanted to accomplish with the Stocks Tracker data. The first goal was to keep any data access or modeling classes isolated in their own projects, accessible only through abstractions, so that any layer(s) reliant on data would have no knowledge of how the underlying data store worked. The second goal was to leverage Microsoft’s Entity Framework (EF) and Identity frameworks.
Models
For data modeling, I added a new class library project to the Stocks Tracker solution, and called it Data.Models. The data model was pretty simple, and only required adding a few NuGet packages, so that EF and Identity could work together:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="EntityFramework" version="6.1.0" targetFramework="net45" /> <package id="Microsoft.AspNet.Identity.Core" version="2.0.1" targetFramework="net45" /> <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.0.1" targetFramework="net45" /> </packages>
The classes were simple POCOs that EF needs to define and map entity relationships to an underlying data store. Pretty standard stuff, if you prefer EF’s code-first approach to generating database schemas. However, throw in Identity and EF will also generate objects used by the Identity framework for security, such as tables to store user accounts, as well as support for third-party logins, such as Google, right out of the box. Which is pretty cool, and necessary for a modern Web application.
The only thing needed was to create a class derived from IdentityUser, and EF is smart enough to create a security schema containing all the IdentityUser properties, as well as any custom properties defined in the derived class. Mine adds the properties FirstName and LastName, which will be added as columns to the table AspNetUsers.
/// <summary>
/// Encapsulates the properties of an application user object.
/// </summary>
public class ApplicationUser : IdentityUser
{
/// <summary>
/// Instantiates the ApplicationUser class.
/// </summary>
public ApplicationUser()
{
}
/// <summary>
/// Gets and sets the first name value.
/// </summary>
public string FirstName { get; set; }
/// <summary>
/// Gets and sets the last name value.
/// </summary>
public string LastName { get; set; }
}
And that’s it for data modeling. Up next, the data context.
[…] https://cornerthehouse.com/2015/09/07/building-a-full-stack-web-application-part-2-modeling-the-data/ […]
LikeLike
[…] https://cornerthehouse.com/2015/09/07/building-a-full-stack-web-application-part-2-modeling-the-data/ […]
LikeLike