Entity Framework In MVC - Part Two

In this article, I will explain how to use code first approach of entity framework in console application C#.

I explained in the previous article how to use database first approach of entity framework in a console application in C#. Today, I will be explaining how to use code first approach of entity framework in console application C#.

So, first, we have to know which Code first approaches to use in entity framework?

In the Code-First approach, when we don’t have an existing database then first we have to create a class according to our object-based entity; i.e. domain class.

Entity Framework

Now, we have to MAP POCO (Plain Old CLR Objects) class to the database. POCO class is a normal plain class. According to the use by our domain class in this POCO class we declare entities or properties.

Open Visual Studio and go to the file and select new project and add a console application. Now, I am going to create a class. Here, I am taking an Employee class and declaring all properties.

  1. public class Employee {  
  2.     public int EmployeeId {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string EmpName {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Address {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string EmailId {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string MobileNo {  
  19.         get;  
  20.         set;  
  21.     }  
  22.     public string DeptName {  
  23.         get;  
  24.         set;  
  25.     }  
  26. }  

Again, we have to add a DbContext class so right-click and add an EmpdbContext class.

  1. public class EmpdbContext {}  

Now, need to inherit from DbContext class,

Entity Framework

Now, we have to add namespace using System.Data.Entity so before adding this namespace we have to install Entity Framework.

So, right-click on the solution explorer and go to nuget package manager and go to Tools option ->New Get Package Manager -> Package manager.

Entity Framework

After that write the command “Install-Package EntityFramwork” in the command window.

Entity Framework

After completing the installation again check the namespace.

Entity Framework

Now, select and add the namespace

Note


The DbContext class is a class that is responsible for interacting with the database. It is responsible for managing the relationship and converting the raw data from the database into the entity object.

Now, we have to add DbSet in DbContext class

  1. public class EmpdbContext: DbContext  
  2.     {  
  3.         public DbSet<Employee> employees { get; set; }  
  4.     }  

Note


DbSet class represents an entity set. Using in this class we can do to create, read, update, and delete operations.
  1. public class EmpdbContext : DbContext  
  2.     {  
  3.         public DbSet<Employee> employees { get; set; }  
  4.     }  

Now, open the program class and go inside the main method and write our program but before that we have to set connection string in App.Config file

  1. <connectionStrings>  
  2.    <add name="EmpdbContext" connectionString="Data Source=.;Initial Catalog=ExampleDB;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient" />  
  3. </connectionStrings>  

After that add namespace in our employee.cs class and add table attribute and Key of primary key attribute.

  1. using System.ComponentModel.DataAnnotations;  
  2. using System.ComponentModel.DataAnnotations.Schema;  
  3.   
  4. namespace ConsoleApplication1  
  5. {  
  6.   
  7.     [Table("Employee")]  
  8.    public class Employee  
  9.     {  
  10.         [Key]  
  11.         public int EmployeeId { get; set; }  
  12.         public string EmpName { get; set; }  
  13.         public string Address { get; set; }  
  14.         public string EmailId { get; set; }  
  15.         public string MobileNo { get; set; }  
  16.         public string DeptName { get; set; }  
  17.     }  
  18. }  

Now, we will go in program.cs class and write the login inside the main method.

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             using (EmpdbContext objConext = new EmpdbContext())  
  6.             {  
  7.                 Employee objEmploye = new Employee();  
  8.   
  9.   
  10.                 Console.WriteLine("Enter Name");  
  11.                 objEmploye.EmpName = Console.ReadLine();  
  12.                 Console.WriteLine("Enter Address");  
  13.                 objEmploye.Address = Console.ReadLine();  
  14.                 Console.WriteLine("Enter Email Id");  
  15.                 objEmploye.EmailId= Console.ReadLine();  
  16.                 Console.WriteLine("Enter Mobile Number");  
  17.                 objEmploye.MobileNo = Console.ReadLine();  
  18.                 Console.WriteLine("Enter Department Name");  
  19.                 objEmploye.DeptName = Console.ReadLine();  
  20.   
  21.                 objConext.employees.Add(objEmploye);  
  22.   
  23.   
  24.                 objConext.SaveChanges();  
  25.   
  26.                 var empDetails = objConext.employees;  
  27.                 Console.WriteLine("     Name" + "         " + "Address" + "    " + "EmailId" + "                 " + "Mobile Number" + "  " + "Dept Name");  
  28.                 Console.WriteLine("------------------------------------------------------------------------------");  
  29.                 foreach (var item in empDetails)  
  30.                 {  
  31.                     Console.WriteLine(item.EmpName + "  | " + item.Address + " |  " + item.EmailId + "  |  " + item.MobileNo + " |  " + item.DeptName);  
  32.                 }  
  33.             }  

Before running the project check in data base if the table is created or not. When we will run the project then it will automatically create a table inside the database based on our domain class.

Similarly, we fill in all properties,

Entity Framework

Now, go our database and check again if the table is created or not?

Entity Framework

And check records also,

Entity Framework

Now, see the Final Output in our console window,

Entity Framework

Summary

In this article, we saw a brief introduction of code first approach in Entity Framework and how to implement it. And also we saw how to insert records into the database and retrieve the data from the database. In the next article, we will see about the code first approach in Entity framework using MVC.