How to Perform Add Range Operation using Entity Framework

Introduction

In this blog we will see how to perform add range operation using entity framework.

Step 1: Create console application

Employee.cs

  1. namespace AddRangeEFApp  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.     using System.ComponentModel.DataAnnotations;  
  6.     using System.ComponentModel.DataAnnotations.Schema;  
  7.     using System.Data.Entity.Spatial;  
  8.   
  9.     [Table("Employee")]  
  10.     public partial class Employee  
  11.     {  
  12.         public int Id { getset; }  
  13.   
  14.         [StringLength(50)]  
  15.         public string FirstName { getset; }  
  16.   
  17.         [StringLength(50)]  
  18.         public string LastName { getset; }  
  19.     }  
  20. }

Employeecontext.cs

  1. namespace AddRangeEFApp  
  2. {  
  3.     using System;  
  4.     using System.Data.Entity;  
  5.     using System.ComponentModel.DataAnnotations.Schema;  
  6.     using System.Linq;  
  7.   
  8.     public partial class EmployeeContext : DbContext  
  9.     {  
  10.         public EmployeeContext()  
  11.             : base("name=EmpConn")  
  12.         {  
  13.         }  
  14.   
  15.         public virtual DbSet<Employee> Employees { getset; }  
  16.   
  17.         protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  18.         {  
  19.             modelBuilder.Entity<Employee>()  
  20.                 .Property(e => e.FirstName)  
  21.                 .IsUnicode(false);  
  22.   
  23.             modelBuilder.Entity<Employee>()  
  24.                 .Property(e => e.LastName)  
  25.                 .IsUnicode(false);  
  26.         }  
  27.     }  
  28. }

Web.config

  1. <connectionStrings>  
  2.   <add name="EmpConn" connectionString="data source=WIN-B4KJ8JI75VF;initial catalog=EmployeeDB;user id=sa;password=India123;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />  
  3. </connectionStrings>

Program.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace AddRangeEFApp  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             IList<Employee> newEmployee = new List<Employee>();  
  14.             newEmployee.Add(new Employee() { FirstName = "Jon", LastName = "Aw" });  
  15.             newEmployee.Add(new Employee() { FirstName = "Syed", LastName = "Khan" });  
  16.             newEmployee.Add(new Employee() { FirstName = "James", LastName = "Still" });  
  17.   
  18.             using (var objEmpContext = new EmployeeContext())  
  19.             {  
  20.                 objEmpContext.Employees.AddRange(newEmployee);  
  21.                 objEmpContext.SaveChanges();  
  22.             }  
  23.   
  24.             Console.ReadKey();  
  25.         }  
  26.     }  
  27. }

Output of the application looks like this

Summary

In this blog we have seen how we can perform add range operation using entity framework. Happy coding!