Entity Framework Code First Approach With Database Migration

Entity Framework

Entity Framework in an ASP.NET is the enhancement of ADO.NET, which provides the strong mapping facilities. It is an ORM (Object Relational Mapping) framework, which gives the ability to the developers to work with various relational databases like SQL Server, Oracle, MYSQL, DB2 etc. and provide the automated mechanism for accessing & storing the data in the database.

There are the 3 approaches given below, which we use during an Application development.

  1. First, if you want to focus on your domain classes and subsequently create the database from your domain classes; it is called Code First Approach.
  2. Second, if you already have an existing database and create the domain classes from the database; it is called Database First Approach.
  3. Third, you want to design your database schema on Visual Designer, followed by creating the database and classes called Model First Approach.

Thus, in this article, we will learn how to develop a simple C# Application, using Entity Framework Code First approach. 

Installation

Step 1

After downloading completely, click Install button and it will start the installation process.

 
Step 2

Once the installation process completes successfully, you will see the dialog given below. Close this dialog and restart your computer, if required.

 

Step 3

Once everything is done, you will see the main Window of Visual Studio.

 

Let’s create a new project from File → New → Project.

 

Go to other Project Types, select Visual Studio Solution and select Blank Soluton. Change solution name CodeFirstApproach, followed by clicking OK button.

Note:- One solution has multiple projects .

Right click on Solution Explorer and New Project and you can see below.

 

Select class library, followed by clicking OK.

 

Right click on the project in Solution Explorer and select Manage NuGet Packages.

 

This will open Manage NuGet packages dialogue box. Now, select Browse on top bar and search for an EntityFramework as shown below.

 

Select EntityFramework and click Install.

 

Click I Accept button in License Acceptance dialogue box. This will start the installation.

 

Now, add a new class file, Employee.cs and declare four properties: Id, LastName, FirstName and BirthDate, as shown below.

  
  1. public class Employee  
  2. {  
  3.     [DatabaseGenerated(DatabaseGenerationOption.Identity)]  
  4.     [Key]  
  5.     public int Id {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     public string LastName {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string FirstName {  
  14.         get;  
  15.         set;  
  16.     }  
  17.     public DateTime BirthDate {  
  18.         get;  
  19.         set;  
  20.     }  
  21. }  

 

Add a new class MyContext.cs and let it inherit from DbContext class (add System.Data.Entity as using).

  

Now, we will add the connection string in App.config.

  1. <connectionStrings>  
  2.     <add name="Dbconnection" connectionString="Data Source=DHRUV-PC\SQLEXPRESS;Initial Catalog=EmployeeDb;Integrated Security=True" providerName="System.Data.SqlClient" />   
  3.  </connectionStrings>  

Note

EmployeeDb is the database name, if I use migration and the database will be created automatically i.e. EmployeeDb name.

 

Now, see Code First Migrations

From the Tools menu, click Library Package Manager and then Package Manager Console then choose the default project EntityDomain in it. That means always choose the project with your MyContext class for migrations.

At PM> enter the command given below.

PM> enable-migrations

When running the preceding command, you will get a console Window, as shown below.  

This command adds a new folder, Migrations in the project EntityDomain and this folder contains a configuration file with default settings.

  

Now, we add to the configuration settings in the Configuration class constructor, i.e. one to allow migration and another for no data loss when migrating. The excerpt of the Configuration class for these properties are given below. 

  1. AutomaticMigrationsEnabled = true;  
  2. AutomaticMigrationDataLossAllowed = false;   

We set the AutomaticMigrationEnabled property to true; it means that we are using automatic code first migration and another property AutomaticMigrationDataLossAllowed is set to false. This means that during the migration, no existing data is lost from the migration of the table of the database. The entire Configuration class is shown below.

 

Thereafter, we will update the database, using Package Manager Console. To update the database at the PM, prompt the command given below.

PM> Update-Database

 

Now, check in the database. We find that in the database, EmployeeDb with Employees table.

 

Note

Database name which is provided in app.config and table name, which is the same name as Employee class name.

I hope, this article will provide a clear understanding of how to use Entity Framework in our Application.

I hope, you enjoyed the valuable code. Happy coding.


Similar Articles