Library Management using ASP.Net MVC (Part 3) Adding the Customers

In this article you will learn about Entity Framework code first migration. In the first part we use Entity Framework code first to create the book model. Code first allows us to focus on creating classes for our domain requirements rather than designing a database first then create a classes to match our database.

We will follow the same instruction that we did in adding the Book in the first part. In the Models folder create a Customer class with these properties.

using System.ComponentModel.DataAnnotations;

namespace LibraryManagement.Models
{
    public class Customer
    {
        public int CustomerId { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public string Address { get; set; }

        [Required]
        public string Contact { get; set; }

    }
}

Then update the code in ApplicationDbContext inside the IdentityModel.cs to add the Customer model.

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; }
    }

Build your project by pressing CTRL + SHIFT + B then right click to Controllers folder and make a scaffold of Customer CRUD function. If your not sure how to do it, follow the instruction in the first part Library Management using ASP.Net MVC (Part 1) Books CRUD Function.

We also need to add the Customer link in our navigation bar just like what we did in the book at the first part. Add this line of code in the navigation list.

	<li>@Html.ActionLink("Customers", "Index", "Customers")</li>

Now run the application and click the link for the customers page. This time you would definitely get this error.

migration error

Entity framework detected that we changed our ApplicationDbContext and the current version of our ApplicationDbContext didn’t match the database. To fix this we need to enable migration, add migration then update the database to match our current context.

Click tools then NuGet Package Manager then click Package Manager Console.

nuget package manager

Then in Package Manager Console execute the command

	enable-migrations

After the executing the enable-migrations. Execute the command add-migration.

	add-migration

Then it will ask for a name of the migration. Let’s give it some sensible name. Typed in “Add Customer Model” then press enter.

After that run the command update-database. To update our database.

	update-database

After running these commands, run your application again and navigate to customers index page. Everything should be working well now. Try to add some customers in the list.

Customers index page

In the next part we will implement the borrowing functionality of our library management system.

Related Articles

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s