Click here to Skip to main content
15,847,750 members
Articles / Web Development / ASP.NET

MVC and Entity Framework Code First, Code Migration approach

Rate me:
Please Sign up or sign in to vote.
4.88/5 (6 votes)
27 Sep 2015CPOL7 min read 21.4K   18   1
Learn and build web application using MVC and Entity Frame work code first, code migration approach.

Learn and build web application using MVC and Entity framework code first, code migration approach

Learn MVC and Entity framework code first,code migration approach using sample demo application

Entity Framework

  • Entity Framework is an Object Relational Mapping (ORM) framework that enables developers to work with relational data has a domain specific object, eliminating the use of most of the data access plumbing code that developers usually need to write.
  • Entity Framework is an enhancement to that gives developers an automated mechanism for accessing and storing the data.
  • Developers can write Linq queries for getting the data from the database using Entity Framework.
  • Entity Framework helps us to retrieve and manipulate data as strongly typed objects.

Approaches in Entity Framework

  • Model First: database model is created first using the ORM designer tool in Visual Studio. Once the model consisting of entities and relationships has been designed, the physical database and classes will be auto generated from the model with the help of Entity Framework.
  • Data Base First: Database is created first or we can use the existing database and then code will be auto generated with the help of Entity Framework.
  • Code First: Create a set of classes i.e. code first and the database will be auto generated by the help of Entity Framework.

Why Entity Framework

  • There are 2 types of model or groups like
    • Logical or Storage Model – deals with tables, primary key, foreign key etc. which will be handled by 1 group of people called as database centric
    • Conceptual or Object Oriented Domain Model - deals with objects, properties, inheritance polymorphism etc. which will be handled by 1 group of people called as application centric
  • As a result of these 2 models Impedance mismatch occurs and developers devote a lot of time and energy in writing a data access plumbing code like for mapping and inserting it to the database
  • Entity framework seeks to remedy to problem by providing the layer of abstraction between the logical data model and the application domain model

Entity Framework Code First Migration Approach

  • Code First is mainly used in Domain Driven Development (DDD) approach. We can focus on the domain design and start writing the classes based on the domain requirements
  • Code First API will create a database on the fly based on your entity class and configuration
  • Code First Entity framework 4.2 or previous version we need to use any of the database initializers methods and handle to seed or maintain data when there will be a change in the model
  • Code First Migration approach is very handy when compared to previous versions, as it is able to track the changes and handle the model changes smartly. Without the need to think about the dropping and recreating data
  • Code First Migration allows you to create a new db or update existing db based on your model classes by using package manager console present in visual studio

Advantages of using code first Entity Framework

  • Entity Framework enables developers to create data access application by programming against conceptual application model instead of programming against relational data storage schema
  • Application can work more in terms of Application centric conceptual model including types like inheritance, complex members and mappings
  • Entity Framework is an ORM tool that provides simple API to perform CRUD operations
  • Entity Framework also helps us to perform certain DDL and DML operations from the front end, rather than manually doing it in the back end
  • Entity Framework provides strongly typed classes, giving
    • Intellisense support,
    • compile time and
    • debugger option
  • Entity Framework automatically does the plumbing and mapping for you. So we developers need not to write most of the data access plumbing code i.e. usually required(e.g.
  • Lot of time is will be saved and it helps us to quickly develop the application and concentrate more on the Business centric details

MVC and Entity Framework Code First, Code Migration Approach

Create Project and Enable Entity framework code first migration approach

Video :

  • Create a new MVC 4 web application.
  • Give the name of the application and click on OK.

Image 1

  • Select the required template and the View Engine.

Image 2

  • Goto tools -> Library Package Manager -> Package Manger Console.
  • Install entity framework - 5 in package manager console by executing the command
    • install-package entityFramework -version

Image 3

  • Create a new class for DBContext which acts like a bridge or connection object between database and the LINQ object
  • DBContext provides a abstraction layer between Application layer and Database layer.
  • DBSet will represent the table which will be present in the database.

Image 4

  • Open the web.Config file and provide the required ConnectionString.
  • ContextName will be same as the 1 which we created earlier.
  • Also give the proper connecion string details like data source, Initial catalog and provider name etc.

Image 5

  • Goto Package Manager Console.
  • Enable the migrations for using Code First Code Migrations approach by executing the command
    • Enable-Migrations -ContextTypeName DNSContext
  • Code-First Migrations will take care of the data. So we no need to write the dropcreate database methods that was used in earlier old version of Entity frame work.

Image 6

  • Once after enabling the migrations for the particular context we are using, we have to Add Migration.
  • Add Migration will generate Migration file, sql code and this will not be executed in SQL Server unless and until we run "update-database" command.

Image 7

  • Code Migration File Generated.

Image 8

  • "update-database" command will seed the database by running the code which is present in the code migration file.
  • Since we are running it for the first time,new database with name provided in the connection string of initial catalog will be created in the database

Image 9

  • Create a new Course Model with required entities in the file.

Image 10

  • Add a DbSet property in context class
  • Add-Migration and update the database

Image 11

Create CRUD operations using scaffolding template

Video :

  • Add a Course Controller.
  • Select the scaffolding template "MVC controller with read/write actions and views, using Entity Framework".
  • Select the Data Context class.
  • CRUD operations in the Course Controller will be created with corresponding views will be created using scaffolding templates and Entity framework methods.

Image 12

  • Add a ActionLink in the Nav bar for the Course.

Image 13

Create a dropdownlist in MVC

  • Let us add the dropdown to the domain entity with values hardcoded in the view

Image 14

  • Course screen after

Image 15

  • If you want the Hardcoded values to get it from controller

Image 16

  • Provide the ViewBag in the View

Image 17

Create a Model for Student screen and update database using code Migration


  • Create a new model for Student Page.

Image 18

  • Update the DBContext by adding the new property for student

Image 19

  • Now add a new migration for creating Student table in database
  • use "Add-Migration" command for creating the migration file

Image 20

  • update the database using "Update-database" command in package manger console
  • Create a new Controller for Student with readWrite action selected and add the controller

Image 21

Create operation in MVC

Video :

  • Student controller gets created
  • Add a View for Create screen

Image 22

  • Run the application and goto "Student/Create" page

Image 23

  • Create an object for DNSContext
  • Create a HttpPost method for inserting the data into db.

Image 24

Render operation in MVC

Video :

  • Retrieve operation in MVC.
  • Add a view to display the list of students in the view

Image 25

Image 26

  • Modify the index page to display all the data in the list

Image 27

Update operation in MVC


  • Edit or update operation in MVC

Image 28

  • post operation in MVC for updating the Student record.

Image 29

  • Detail operation

Image 30

Delete operation in MVC


  • Delete operation action method.

Image 31

Image 32

  • Finally Student page is created with all CRUD operations done.

Image 33

Configure one To Many and Many to Many relationship in MVC and Entity Framework Code migration approach


Configure Many to Many relationship in MVC and Entity Framework Code migration approach



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer HP
India India
I am working in HP as a dot net developer.Working in .net technologies(MVC,Entityframework,LINQ,Jquey

Comments and Discussions

-- No messages could be retrieved (timeout) --