-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathDbContextExtensions.cs
51 lines (43 loc) · 1.69 KB
/
DbContextExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace TestBuildingBlocks;
public static class DbContextExtensions
{
public static void AddInRange(this DbContext dbContext, params object[] entities)
{
dbContext.AddRange(entities);
}
public static async Task ClearTableAsync<TEntity>(this DbContext dbContext)
where TEntity : class
{
await ClearTablesAsync(dbContext, typeof(TEntity));
}
public static async Task ClearTablesAsync<TEntity1, TEntity2>(this DbContext dbContext)
where TEntity1 : class
where TEntity2 : class
{
await ClearTablesAsync(dbContext, typeof(TEntity1), typeof(TEntity2));
}
private static async Task ClearTablesAsync(this DbContext dbContext, params Type[] models)
{
foreach (Type model in models)
{
IEntityType? entityType = dbContext.Model.FindEntityType(model);
if (entityType == null)
{
throw new InvalidOperationException($"Table for '{model.Name}' not found.");
}
string? tableName = entityType.GetTableName();
if (tableName == null)
{
// There is no table for the specified abstract base type when using TablePerConcreteType inheritance.
IEnumerable<IEntityType> derivedTypes = entityType.GetConcreteDerivedTypesInclusive();
await ClearTablesAsync(dbContext, derivedTypes.Select(derivedType => derivedType.ClrType).ToArray());
}
else
{
await dbContext.Database.ExecuteSqlRawAsync($"delete from \"{tableName}\"");
}
}
}
}