Remove all records from a table with Entity framework 6

2

How to delete all data from a database table using EF6 without having to query (do I want to delete all records)?

    
asked by anonymous 20.10.2015 / 17:59

2 answers

5

The simplest way is:

context.Database.ExecuteSqlCommand("TRUNCATE TABLE [Tabela]");

Yes, it does not pass directly through the Entity Framework. To use the Entity Framework explicitly, it is best to implement an extension:

public static void ApagarTudo<T>(this DbSet<T> dbSet) where T : class
{
    dbSet.RemoveRange(dbSet);
}

Usage:

contexto.Entidades.ApagarTudo();
contexto.SaveChanges();
    
20.10.2015 / 18:16
1

You can use the ExecuteStoreQuery () method of your context by passing an SQL command to delete all records, see the example below.

seuContexto.ExecuteStoreQuery("delete SuaTabela");

For more details go to the link below link

    
20.10.2015 / 18:15