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 { get; set; }
  8. public string CustomerName { get; set; }
  9. public List<Address> Address { get; set; }
  10. }
  11. public class Address
  12. {
  13. public int Id { get; set; }
  14. public string Street { get; set; }
  15. public string PinCode { get; set; }
  16. }
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 { get; set; }
  4. public DbSet<Address> Addresses { get; set; }
  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. static void Main(string[] args)
  5. {
  6. GetCustomer("Before Add: ");
  7. AddCustomer();
  8. GetCustomer("After Add: ");
  9. Console.ReadKey();
  10. }
  11. private static void GetCustomer(string text)
  12. {
  13. // Get all data from Customer table
  14. var customers = context.Customers.ToList();
  15. Console.WriteLine($"{text}: Customer Count is {customers.Count}");
  16. foreach (var customer in customers)
  17. {
  18. Console.WriteLine(customer.CustomerName);
  19. }
  20. }
  21. private static void AddCustomer()
  22. {
  23. // For inserting to Customer table
  24. var customer = new Customer { CustomerName = "Swetha" };
  25. var address = new Address { Street = "Bangalore", PinCode = "560091" };
  26. context.Customers.Add(customer);
  27. customer.Address.Add(address);
  28. context.SaveChanges();
  29. }
  30. }

Summary

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