Skip to content

Instantly share code, notes, and snippets.

@Jay-Madden
Created May 19, 2022 18:14
Show Gist options
  • Save Jay-Madden/529b098f26b076ad2639e680110658e2 to your computer and use it in GitHub Desktop.
Save Jay-Madden/529b098f26b076ad2639e680110658e2 to your computer and use it in GitHub Desktop.
Extension method to truncate a postgres table from an ef context
public static async Task TruncateTable<T>(this CersPg context) where T : class
{
var tableAttr = (TableAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(TableAttribute));
var sql = $"TRUNCATE {tableAttr.Name} RESTART IDENTITY CASCADE";
await context.Database.ExecuteSqlRawAsync(sql);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment