Hello Guys, Here we are to learn Code First Approach in Entity Framework. As we have already discussed Entity Framework and ORM technology in detail. Now let’s focus what exactly is Code First Approach in Entity Framework and we will also create a simple example using C#.

After completing this tutorial you will be able to understand:

  • Code First Approach in Entity Framework.
  • How to create a simple example of Code First Approach in Entity Framework with C#.

What is Code First Approach?

As we have already discussed with Code First Approach we can focus on creating classes as per domain requirement rather database design first. This approach simply focuses on the database that does not exist and will create it using Code. Also, this approach avoids us to work with Visual Model Designer (EDMX).

Code First Approach

Why should we use Code First Approach?

There are few situations where we come to use Code First Approach and here is few of them.

  • When you have an empty Database.
  • When you have large numbers of Tables, Stored Procedures and Views.
  • When you have to make your application from scratch.

An example of Code First Approach with C#:

Step 1 – Open Visual Studio and create a Console Application with the name CodeFirstApproachDemo.

Code First Approach

Step 2 – Open Package Manager Console Window as shown in the below image.

Code First Approach

 

Step 3 – Go to Tools> Library Package Manager> Manage NuGet Packages For Solution.

Code First Approach

Step 4 – Type Entity Framework in the Search Box and install the Entity Framework Package.

Code First Approach

Step 5- Right Click on the Solution Explorer and add a class with the name Employee. Write following code in Employee Class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirstApproachDemo
{
 class Employee
 {
 public int EmployeeID { get; set; }
 public string EmployeeName { get; set; }
 public string ContactNo{ get; set; }
 }
}

Step 6- Right Click on the Solution Explorer and add a class with the name DatabaseContext. Write following code in DatabaseContext Class.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirstApproachDemo
{
 class DatabaseContext : DbContext
 {
 public DatabaseContext()
 : base("Data Source=DESKTOP-9VC12LL\\SQLEXPRESS;initial catalog=CodeFirstDb;integrated security=true;")
 { 
 }

public DbSet<Employee> employees { get; set; }
 
 }
}

You can see DatabaseContext class is derived from DbContext and exposes DbSet properties for the types that you want to be part of the model. DbSet is a set of entity classes so it is better to give property name as plural.

Step 7- Now open Program.cs file and write following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirstApproachDemo
{
 class Program
 {
 static void Main(string[] args)
 {
 using (DatabaseContext objDBContext = new DatabaseContext())
 {
 var vEmployee = new Employee() { EmployeeID = 1, EmployeeName = "Vinod", ContactNo = "00000000" };
 objDBContext.employee.Add(vEmployee);
 objDBContext.SaveChanges();
 }
 Console.ReadKey();
 }
 }
}

Now run the application you will surprise to see the database and table are automatically created with one record.

Code First Approach

Code First Approach

You can download the complete source code from here.

That’s all hope you loved this post about Code First Approach in Entity Framework.

You must also see the related topic.

introduction to Entity Framework in.NET

Thank You.

Leave a Comment