How to delete all data from a database table using EF6 without having to query (do I want to delete all records)?
How to delete all data from a database table using EF6 without having to query (do I want to delete all records)?
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();
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