4.0: Extending User Entity

Right now, our User entity is very simple - it is only has the id, username, and password hash/salt for our users.

Let's add some more information for each of our users.

Adding More Properties to User.cs

Update User.cs inside of your .Data/Entities folder to:

public class User
{
    public int Id { get; set; }

    public string Username { get; set; }

    public byte[] PasswordHash { get; set; }

    public byte[] PasswordSalt { get; set; }

    public string Specialty { get; set; }

    public DateTime DateOfBirth { get; set; }

    public string KnownAs { get; set; }

    public DateTime Created { get; set; }

    public DateTime LastActive { get; set; }

    public string Introduction { get; set; }

    public string LookingFor { get; set; }

    public string Interests { get; set; }

    public string City { get; set; }

    public string State { get; set; }
}

We'll also want to add photos for our users - so let's create a new entity to represent them.

Adding Photo.cs

Inside of your .Data/Entities folder, add a new class called Photo.cs:

public class Photo
{
    public int Id { get; set; }

    public string Url { get; set; }

    public string Description { get; set; }

    public DateTime DateAdded { get; set; }

    public bool IsMain { get; set; }

    public User User { get; set; }

    public int UserId { get; set; }
}

This class has a Foreign Key of UserId that will connect each Photo to the User with the matching Id.

It also has a navigation property of User - which will be an object of the user with the Foreign Key. This will aid in querying.

Read more here.

Adding Photo navigation property to User

Now, we'll make use of our new Photo entity and add a navigation property of Photo to our User.cs class.

This time it will be a collection of Photos - because we want our User to have more than one photo (one-to-many relationship).

public ICollection<Photo> Photos { get; set; }

public User()
{
    Photos = new Collection<Photo>();
}

Database Diagram

Here's what our current relationship looks like.

It's a one-to-many relationship between User (one) and Photos (many).

As we continue our application, this will get more interesting as we add more tables and many-to-many relationships.

Last updated