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&lt;ApplicationUser&gt; { 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.
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.
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 the customers index page. Everything should be working well now. Try to add some customers on the list.
In the next part, we will implement the borrowing functionality of our library management system.
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 4) Entity Framework Relationship and Navigation Properties
- 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