Remove Range Entity Framework

Introduction

This article shows how to perform remove range operations using Entity Framework.

Create console application

Employee.cs

  1. namespace RemoveRangeEFApp  
  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.     [Table("Employee")]  
  9.     public partial class Employee  
  10.     {  
  11.         public int Id { getset; }  
  12.         [StringLength(50)]  
  13.         public string FirstName { getset; }  
  14.         [StringLength(50)]  
  15.         public string LastName { getset; }  
  16.     }  
  17. }  

Employeecontext.cs

  1. namespace RemoveRangeEFApp  
  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.     }  

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 RemoveRangeEFApp  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             using (var objEmpContext = new EmployeeContext())  
  14.             {  
  15.                 var employees = objEmpContext.Employees.Where(p => p.FirstName == "Syed").ToList();  
  16.                 objEmpContext.Employees.RemoveRange(employees);  
  17.                 objEmpContext.SaveChanges();  
  18.             }  
  19.   
  20.             Console.ReadKey();  
  21.         }  
  22.     }  
  23. }

The following is the output of the application:

 

Summary

In this article we saw how to do remove range operations using Entity Framework. Happy coding.


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.