EF7 Bulk Delete

    0

    0

    Konrad Psiuk

    This code uses the Microsoft.EntityFrameworkCore NuGet package to create a new ConsoleApp1.Program class and a new AppDbContext class. Main() uses the AppDbContext class to get a list of customers from the Customers property. The code then uses the Where method on the Customers collection to find the customer with the customer ID of "1". The customer is then deleted using the ExecuteDeleteAsync method on the AppDbContext class.

    using Microsoft.EntityFrameworkCore.Relational;
    
    using Microsoft.EntityFrameworkCore;
    
    
    namespace ConsoleApp1
    {
      public class Program
      {
        static async Task Main(string[] args)
        {
          Console.WriteLine("Hello, World!");
          var cont = new AppDbContext(null);
          await cont.Customers.Where(x=>x.CustomerID=="1").ExecuteDeleteAsync();
    
        }
      }
    
      public class Customer
      {
        public string CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string Country { get; set; }
      }
    
      public class AppDbContext : DbContext
      {
        public DbSet<Customer> Customers { get; set; }
    
        public AppDbContext(DbContextOptions
        <AppDbContext> options) : base(options)
        {
    
        }
      }
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.