Month: November 2018

EntityFrameworkCore. Truncating / Emptying a table.

I was in the position of creating unit tests for a custom web api, and needed to wipe all data in a table before I run my tests.

Here’s the small command I run to delete all rows in the table

 

        public void deleteAllData() 
        {
            using (var db = new AlertsDbContext()) 
            {
                db.CUSTOMTABLE.RemoveRange(db.CUSTOMTABLE);
                db.SaveChanges();
            }

        }

Remove range takes IENumerable as a parameter. Which is why when you pass it an entire collection, it’ll straight up delete all entries.