Remove all records from a table with Entity framework 6

Asked

Viewed 1,410 times

2

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

2 answers

5


The simplest way is:

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

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

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

Use:

contexto.Entidades.ApagarTudo();
contexto.SaveChanges();

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.