Created
May 19, 2022 18:14
-
-
Save Jay-Madden/529b098f26b076ad2639e680110658e2 to your computer and use it in GitHub Desktop.
Extension method to truncate a postgres table from an ef context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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