Skip to content

Cleanup

One test’s rows must not leak into the next. Call CleanAsync() at the start of a test:

await harness.CleanAsync();

In a DbTestBase class, harness is Harness.

CleanAsync() empties every user table, whatever the foreign keys between them, and puts every identity column back to its seed. The next row gets the same id on every engine.

The mechanism differs by engine:

A table that no foreign key references is truncated. That empties it and resets its identity to exactly the declared seed, whatever the column type.

SQL Server refuses to truncate a table a foreign key references, even a disabled one. Those tables are emptied with DELETE while every constraint is disabled, and the constraints are re-enabled WITH CHECK so the optimizer still trusts them. Their identity is then reseeded.

  • It does not touch the shape of objects. That is the shape guard’s job.
  • It also empties reference data that a seed script inserted. Seed it again after cleaning. With a SQL Server database project, see the dacpac guide for how the sample does it.