简体   繁体   中英

How can I efficiently delete all records in a table using Entity Framework without using SQL?

I need to remove all records in a given table, using a DbContext in Entity Framework. I need this to be portable, so it can't rely on SQL statements or stored procedures. I've looked through the answers to this question , but none of them meet my requirements or are ideal solutions for a variety of reasons.

I could use the RemoveRange method, ie

DbContext.Table.RemoveRange(all);

But this doesn't scale well, because it selects all entries before deleting, which could take a long, long time with Entity Framework. Iterating through each record and removing them individually with Remove(record) has the same problem.

Using SQL this is simple, using a TRUNCATE command. Even a simple DELETE FROM [TableName] command works, but I don't know how scalable that is.

Is there any solution that uses only Entity Framework (no SQL), and doesn't require selecting all records first before deleting them?

This is currently not something that is possible using Entity Framework. see https://github.com/dotnet/efcore/issues/795

There may be an extension out there that will allow you to do that, but I am not sure it will work will all RDBMS systems.

Let's suppose you have BrandData table with records about some brands:

public class BrandData
    {
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }
    }

Also you've created a dbcontext:

public class MyDbContext : DbContext
    {
        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        {
            Database.Migrate();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<BrandData>().HasData(new BrandData { Id = 999, Name = "LG", Description = "TV brand" });
            
        }
        public DbSet<BrandData> BrandData { get; set; }

    }

Finally, this is the method that deletes brand by its ID:

public async Task DeleteModelAsync(int id)
        {
            var data = _dbContext.ModelData.FirstOrDefault(b => b.Id == id);
            if (data != null)
            {
                _dbContext.ModelData.Remove(data);
                await _dbContext.SaveChangesAsync();
            }
        }

Changes will be done after SaveChangesAsync() method run.

UPDATE

To delete all records:

    var brands = await _dbContext.BrandData.ToListAsync();
    
                    foreach(var brand in brands)
                    {
                        _dbContext.BrandData.Remove(brand);
                    }

    await _dbContext.SaveChangesAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM