Setting Up Entity Framework Using Code First Migrations

Introduction

 
This article demonstrates how we can set up customer Entity framework in .Net applications (console application) with a step by step example.
Download Source code from the given GitHub URL.
 
Tools Used for development 
  • Visual Studio 2019
  • SQL server 
Step 1 - Create Application
  • Create Blank solution CustomerEF
  • Class library .NET Standard

    • CustomerEF.Domain
    • CustomerEF.Data
Step 2 - Adding Classes
 
Add class in CustomerEF.Domain class library
  1. public class Customer  
  2. {  
  3.     public Customer()  
  4.     {  
  5.         Address = new List<Address>();  
  6.     }  
  7.     public int Id { getset; }  
  8.     public string CustomerName { getset; }  
  9.     public List<Address> Address { getset; }  
  10.   
  11. }  
  12.   
  13. public class Address  
  14. {  
  15.     public int Id { getset; }  
  16.     public string Street { getset; }  
  17.     public string PinCode { getset; }  
  18.   
  19. }  
Step 3 - Adding migration and DbContext
 
Install EntityFramework via nuget package manager to CustomerEF.Data Class library
 
Add CustomerContext.cs in CustomerEF.Data 
  1. public class CustomerContext : DbContext  
  2. {  
  3.     public DbSet<Customer> Customers { getset; }  
  4.     public DbSet<Address> Addresses { getset; }  
  5. }  
In Package Manager console run the below command, under CustomerEF.Data
 
enable-migrations
 
Add constructor with db connection string  in CustomerContext.cs 
  1. public class CustomerContext : DbContext    
  2. {    
  3.     public CustomerContext() : base("Data Source=(local)\\SQLexpress;Initial Catalog=CustomerEFCORE;Integrated Security=True")    
  4.     {    
  5.         Database.SetInitializer(new MigrateDatabaseToLatestVersion<CustomerContext, Configuration>());    
  6.     }   
  7.     //...


  8. }  
In Package Manager console run the below command, under CustomerEF.Data,
  • add-migration initial-setup
  • update-database
Step 4 - Create Console App
  • Create console app and make it like a default project 
  • Add EntityFramework via Nuget package manager (We can implement Repository Design pattern to make the code better)
  1. class Program  
  2. {  
  3.     static CustomerContext context = new CustomerContext();  
  4.   
  5.     static void Main(string[] args)  
  6.     {  
  7.         GetCustomer("Before Add: ");  
  8.         AddCustomer();  
  9.         GetCustomer("After Add: ");  
  10.         Console.ReadKey();  
  11.     }  
  12.   
  13.     private static void GetCustomer(string text)  
  14.     {  
  15.         // Get all data from Customer table  
  16.         var customers = context.Customers.ToList();  
  17.   
  18.         Console.WriteLine($"{text}: Customer Count is {customers.Count}");  
  19.   
  20.         foreach (var customer in customers)  
  21.         {  
  22.             Console.WriteLine(customer.CustomerName);  
  23.         }  
  24.     }  
  25.   
  26.     private static void AddCustomer()  
  27.     {  
  28.         // For inserting to Customer table  
  29.         var customer = new Customer { CustomerName = "Swetha" };  
  30.         var address = new Address { Street = "Bangalore", PinCode = "560091" };  
  31.         context.Customers.Add(customer);  
  32.         customer.Address.Add(address);  
  33.         context.SaveChanges();  
  34.     }  
  35.   
  36. }  

Summary

 
In this article, I discussed how we can set up Entity framework in console applications with a step by step example.


Similar Articles