ASP MVC CRUD Using JQuery Ajax and Entity Framework Part 1 (Creating The Entity Framework Code First Migration)

asp-mvc-part-1

To learn a technology, better we try to create the basic mechanism in storing and managing data such as CRUD (Create, Read, Update, Delete) First, it will help us to find the way how to get familiar with the code and at least we know the basic knowledge of it. And finally i’ve finished to create this example of using some of technologies that i really like. This time I’m using these technologies :

  • ASP MVC 4
  • Entity Framework Code First
  • Twitter Bootstrap 3
  • JQuery

This blog post is for who don’t know how to get and post request using Ajax in ASP and want to know how the mechanism of Entity Framework in C#. There are 3 types of Entity Frameworks : Code First, Database First, and Model First. You can follow this link to find the differences and each advantages among those Entity Framework types.

But  why i’m using Entity Framework Code First ? Because i have full control over the code, since i don’t like to get in SQL Manager for a while, and get back to Visual Studio to mapping the column, then get back again to SQL Manager to check the schema, and so on. Code first will automatically migrate SQL Query based on our configuration and model, or we can generate the migration files manually. I Prefer to generate the migration files manually, because by the time when we Alter, Drop, or whatever you do to change the schema, we have the history migration of those changes.

Since we have two focuses there are Entity Framework and JQuery Ajax, i divide this tutorial into several parts . In this part, lets trying to create the Backend (Repository and Service).

First, we have to decide what model that we want to create. Lets try to use the simplest schema with table User :

After that lets install the Entity Framework over the nuget package console.

install-EF-nuget

Congratulation, now you have installed your first time Entity Framework :). Then we need to set our connection string. Now, open your Web.config and set your connection string like this.

Now, we need to create dbContext, then set our column configuration. FYI, the default generated table will be ended with s, as for example User become Users. To avoid that problem we need to add this code modelBuilder.Conventions.Remove();.

Then we need to enable migration to let the Entity Framework generate the migration file manually.

enable-migration-EF-nuget

After the migration is enabled. Lets do the magic. This is the way to generate your migration file.

add-migration-EF-nuget

Ahaa… Check your migration files under migrations folder. The migrated file will be like this.

Ensure your configuration is correct. Then do the best part. Using -Verbose that means the table changes will be shown by the package manager console.

update-db-EF-nuget

When everything goes well. Check your SSMS and see the magic happened.

schema-EF

Congratulation your model and configuration have generated into the table. Now you are having the table and dbContext to continue creating ASP MVC CRUD Using Ajax, Entity Framework and Bootstrap. Please waiting for the next part, i’ll write it soon.

FatkhanFauzi About FatkhanFauzi

A passionate software engineer who wants to share anything that he learns and some of his life experiences. Want to know more about him? see Fatkhan Fauzi Profile


2 Comments

So, what do you think ?

Leave a Reply

%d bloggers like this: