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; } [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.
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.
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
- Library Management using ASP.Net MVC (Part 1) Books CRUD Function
- Library Management using ASP.Net MVC (Part 2) Request Lifecycle
- Library Management using ASP.Net MVC (Part 3) Adding the Customers
- Library Management using ASP.Net MVC (Part 5) Eager Loading and Projection
- Library Management using ASP.Net MVC (Part 6) Borrowing and returning a book
- Library Management using ASP.Net MVC (Part 6) Borrowing and returning a book
I have problem to create overload Customer class again:
public int Borrower { get; set; }
[ForeignKey(“Borrower”)]
public Customer Customer { get; set; }
LikeLike
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?
LikeLike