1+ namespace EfLocalDb ;
2+
3+ public static partial class DbContextExtensions
4+ {
5+ internal static IEnumerable < object > ExpandEnumerable ( IEnumerable < object > entities , IReadOnlyList < IEntityType > entityTypes )
6+ {
7+ foreach ( var entity in entities )
8+ {
9+ if ( entity is IEnumerable enumerable )
10+ {
11+ var entityType = entity . GetType ( ) ;
12+ if ( entityTypes . Any ( _ => _ . ClrType != entityType ) )
13+ {
14+ foreach ( var nested in enumerable )
15+ {
16+ yield return nested ;
17+ }
18+
19+ continue ;
20+ }
21+ }
22+
23+ yield return entity ;
24+ }
25+ }
26+
27+ public static Task AddData < TDbContext > ( this TDbContext context , IEnumerable < object > entities )
28+ where TDbContext : DbContext =>
29+ context . AddData ( entities , context . Model . GetEntityTypes ( ) . ToArray ( ) ) ;
30+
31+ public static Task AddData < TDbContext > ( this TDbContext context , params object [ ] entities )
32+ where TDbContext : DbContext =>
33+ context . AddData ( ( IEnumerable < object > ) entities ) ;
34+
35+ internal static Task AddData < TDbContext > ( this TDbContext context , IEnumerable < object > entities , IReadOnlyList < IEntityType > entityTypes )
36+ where TDbContext : DbContext
37+ {
38+ foreach ( var entity in ExpandEnumerable ( entities , entityTypes ) )
39+ {
40+ context . Add ( entity ) ;
41+ }
42+
43+ return context . SaveChangesAsync ( ) ;
44+ }
45+
46+ public static Task RemoveData < TDbContext > ( this TDbContext context , IEnumerable < object > entities )
47+ where TDbContext : DbContext =>
48+ context . RemoveData ( entities , context . Model . GetEntityTypes ( ) . ToArray ( ) ) ;
49+
50+ public static Task RemoveData < TDbContext > ( this TDbContext context , params object [ ] entities )
51+ where TDbContext : DbContext =>
52+ context . RemoveData ( ( IEnumerable < object > ) entities ) ;
53+
54+ internal static Task RemoveData < TDbContext > ( this TDbContext context , IEnumerable < object > entities , IReadOnlyList < IEntityType > entityTypes )
55+ where TDbContext : DbContext
56+ {
57+ foreach ( var entity in ExpandEnumerable ( entities , entityTypes ) )
58+ {
59+ context . Remove ( entity ) ;
60+ }
61+
62+ return context . SaveChangesAsync ( ) ;
63+ }
64+ }
0 commit comments