Library Management using ASP.Net MVC (Part 4) Entity Framework Relationship and Navigation Properties

In this article, we will start looking at the relationship between the customer and book model and how we can keep track of customer borrowing history.

Before we start coding we should determine first the relationship of the customer and the book.

Points to consider

  • The customer can borrow multiple books.
  • The book can be borrowed by a customer multiple times, but not at the same time.

We will design our model base on the above points. If we analyze the points above we can determine that a customer can relate to multiple books and a book can also relate to multiple customers. This kind of relationship is called many to many relationship.

Let’s create our model for borrowing history. In the Models folder create a new class called BorrowHistory then copy and paste the code below.

using System;
using System.ComponentModel.DataAnnotations;

namespace LibraryManagement.Models
    public class BorrowHistory
        public int BorrowHistoryId { get; set; }

        public int BookId { get; set; }

        public Book Book { get; set; }

        [Display(Name = "Customer")]
        public int CustomerId { get; set; }

        public Customer Customer { get; set; }

        [Display(Name = "Borrow Date")]
        public DateTime BorrowDate { get; set; }

        [Display(Name = "Return Date")]
        public DateTime? ReturnDate { get; set; }

In this class we use the BookId and CustomerId as a foreign key for Book and Customer Model. Now you might be wondering how entity framework knows which property to use for foreign key.

The Book and Customer property is called navigation property, we can use these properties to access the data from Book and Customers table.

Now let’s add our BorrowHistory model to our ApplicationDbContext inside the IdentityModel.cs in Models folder.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)

        public static ApplicationDbContext Create()
            return new ApplicationDbContext();

        public DbSet<Book> Books { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<BorrowHistory> BorrowHistories { get; set; }

Now let’s scaffold the controller and views for the BorrowHistory like what we did in Library Management using ASP.Net MVC (Part 1) Books CRUD Function. Build the project, right click to Controllers folder, add controller then Scaffold the controller and views.

Then add migration using the command below, it will ask for a name and we can call it “Add Borrow History”.


Then update the database by using this command.


Run your application now and go to /borrowhistories/create. You will see that BookId and CustomerId have a dropdown and it’s populated by the data from Books and Customers table.

ef relationship

Though this user interface is working, this is not what we want. In the next part we will start modifying the code and create our own view for a better user interface.

Related Articles

2 thoughts on “Library Management using ASP.Net MVC (Part 4) Entity Framework Relationship and Navigation Properties

  1. I have problem to create overload Customer class again:
    public int Borrower { get; set; }

    public Customer Customer { get; set; }


    1. sorry for late reply I was so busy for the past few months with other project. I will revive this blog once again. do you still nee my help to solve this? can you give more info like what your entire Customer class looks like?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s