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

In this article we will start looking at the relationship of the customer and book model and how we can keep track on 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 a 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; }

        [Required]
        [Display(Name="Book")]
        public int BookId { get; set; }

        public Book Book { get; set; }

        [Required]
        [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. By convention they will use the property Id as their foreign key, but you can override it by adding the ForeignKey data annotation, like the code below.

public int Borrower { get; set; }

[ForeignKey("Borrower")]
public Customer Customer { get; set; }

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”.

add-migration

Then update the database by using this command.

update-database

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

Advertisements

2 Responses

  1. Trang

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

    [ForeignKey(“Borrower”)]
    public Customer Customer { get; set; }

    Like

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s