@@ -12,116 +12,147 @@ namespace PhenX.EntityFrameworkCore.BulkInsert.Extensions;
1212public static class DbSetExtensions
1313{
1414 /// <summary>
15- /// Executes a bulk insert operation returning the inserted/updated entities, from the DbSet.
15+ /// Executes a bulk insert operation returning the inserted/updated entities, from the DbSet (synchronous variant) .
1616 /// </summary>
17- public static async Task < List < T > > ExecuteBulkInsertReturnEntitiesAsync < T > (
17+ public static List < T > ExecuteBulkInsertReturnEntities < T > (
1818 this DbSet < T > dbSet ,
1919 IEnumerable < T > entities ,
2020 Action < BulkInsertOptions > ? configure = null ,
21- OnConflictOptions < T > ? onConflict = null ,
22- CancellationToken ctk = default
21+ OnConflictOptions < T > ? onConflict = null
2322 ) where T : class
2423 {
25- var provider = InitProvider ( dbSet , configure , out var context , out var options ) ;
26- var tableInfo = dbSet . GetDbContext ( ) . GetTableInfo < T > ( ) ;
24+ return dbSet . ExecuteBulkInsertReturnEntitiesCoreAsync ( true , entities , configure , onConflict , default ) . GetAwaiter ( ) . GetResult ( ) ;
25+ }
26+
27+ /// <summary>
28+ /// Executes a bulk insert operation returning the inserted/updated entities, from the DbContext (synchronous variant).
29+ /// </summary>
30+ public static List < T > ExecuteBulkInsertReturnEntities < T > (
31+ this DbContext dbContext ,
32+ IEnumerable < T > entities ,
33+ Action < BulkInsertOptions > ? configure = null ,
34+ OnConflictOptions < T > ? onConflict = null
35+ ) where T : class
36+ {
37+ var dbSet = dbContext . Set < T > ( ) ?? throw new InvalidOperationException ( $ "DbSet of type { typeof ( T ) . Name } not found in DbContext.") ;
2738
28- return await provider . BulkInsertReturnEntities ( false , context , tableInfo , entities , options , onConflict , ctk ) ;
39+ return dbSet . ExecuteBulkInsertReturnEntitiesCoreAsync ( true , entities , configure , onConflict , default ) . GetAwaiter ( ) . GetResult ( ) ;
2940 }
3041
3142 /// <summary>
3243 /// Executes a bulk insert operation returning the inserted/updated entities, from the DbContext.
3344 /// </summary>
34- public static async Task < List < T > > ExecuteBulkInsertReturnEntitiesAsync < T > ( this DbContext dbContext , IEnumerable < T > entities , Action < BulkInsertOptions > ? configure = null , OnConflictOptions < T > ? onConflict = null , CancellationToken cancellationToken = default ) where T : class
45+ public static Task < List < T > > ExecuteBulkInsertReturnEntitiesAsync < T > (
46+ this DbContext dbContext ,
47+ IEnumerable < T > entities ,
48+ Action < BulkInsertOptions > ? configure = null ,
49+ OnConflictOptions < T > ? onConflict = null ,
50+ CancellationToken ctk = default
51+ ) where T : class
3552 {
36- var dbSet = dbContext . Set < T > ( ) ;
37- if ( dbSet == null )
38- {
39- throw new InvalidOperationException ( $ "DbSet of type { typeof ( T ) . Name } not found in DbContext.") ;
40- }
53+ var dbSet = dbContext . Set < T > ( ) ?? throw new InvalidOperationException ( $ "DbSet of type { typeof ( T ) . Name } not found in DbContext.") ;
4154
42- return await dbSet . ExecuteBulkInsertReturnEntitiesAsync ( entities , configure , onConflict , cancellationToken ) ;
55+ return dbSet . ExecuteBulkInsertReturnEntitiesCoreAsync ( false , entities , configure , onConflict , ctk ) ;
4356 }
4457
4558 /// <summary>
46- /// Executes a bulk insert operation without returning the inserted/updated entities, from the DbSet.
59+ /// Executes a bulk insert operation returning the inserted/updated entities, from the DbSet.
4760 /// </summary>
48- public static async Task ExecuteBulkInsertAsync < T > (
61+ public static Task < List < T > > ExecuteBulkInsertReturnEntitiesAsync < T > (
4962 this DbSet < T > dbSet ,
5063 IEnumerable < T > entities ,
5164 Action < BulkInsertOptions > ? configure = null ,
5265 OnConflictOptions < T > ? onConflict = null ,
5366 CancellationToken ctk = default
5467 ) where T : class
68+ {
69+ return dbSet . ExecuteBulkInsertReturnEntitiesCoreAsync ( false , entities , configure , onConflict , ctk ) ;
70+ }
71+
72+ private static async Task < List < T > > ExecuteBulkInsertReturnEntitiesCoreAsync < T > (
73+ this DbSet < T > dbSet ,
74+ bool sync ,
75+ IEnumerable < T > entities ,
76+ Action < BulkInsertOptions > ? configure ,
77+ OnConflictOptions < T > ? onConflict ,
78+ CancellationToken ctk
79+ ) where T : class
5580 {
5681 var provider = InitProvider ( dbSet , configure , out var context , out var options ) ;
57- var tableInfo = dbSet . GetDbContext ( ) . GetTableInfo < T > ( ) ;
5882
59- await provider . BulkInsert ( false , context , tableInfo , entities , options , onConflict , ctk ) ;
83+ var enumerable = provider . BulkInsertReturnEntities ( sync , context , dbSet . GetDbContext ( ) . GetTableInfo < T > ( ) , entities , options , onConflict , ctk ) ;
84+
85+ var result = new List < T > ( ) ;
86+ await foreach ( var item in enumerable . WithCancellation ( ctk ) )
87+ {
88+ result . Add ( item ) ;
89+ }
90+
91+ return result ;
6092 }
6193
6294 /// <summary>
63- /// Executes a bulk insert operation without returning the inserted/updated entities, from the DbContext.
95+ /// Executes a bulk insert operation returning the inserted/updated entities, from the DbContext.
6496 /// </summary>
65- public static async Task ExecuteBulkInsertAsync < T > ( this DbContext dbContext , IEnumerable < T > entities , Action < BulkInsertOptions > ? configure = null , OnConflictOptions < T > ? onConflict = null , CancellationToken cancellationToken = default ) where T : class
97+ public static IAsyncEnumerable < T > ExecuteBulkInsertReturnEnumerableAsync < T > (
98+ this DbContext dbContext ,
99+ IEnumerable < T > entities ,
100+ Action < BulkInsertOptions > ? configure = null ,
101+ OnConflictOptions < T > ? onConflict = null ,
102+ CancellationToken ctk = default
103+ ) where T : class
66104 {
67- var dbSet = dbContext . Set < T > ( ) ;
68- if ( dbSet == null )
69- {
70- throw new InvalidOperationException ( $ "DbSet of type { typeof ( T ) . Name } not found in DbContext.") ;
71- }
105+ var dbSet = dbContext . Set < T > ( ) ?? throw new InvalidOperationException ( $ "DbSet of type { typeof ( T ) . Name } not found in DbContext.") ;
72106
73- await dbSet . ExecuteBulkInsertAsync ( entities , configure , onConflict , cancellationToken ) ;
107+ return dbSet . ExecuteBulkInsertReturnEnumerableAsync ( entities , configure , onConflict , ctk ) ;
74108 }
75109
76110 /// <summary>
77- /// Executes a bulk insert operation returning the inserted/updated entities, from the DbSet (synchronous variant) .
111+ /// Executes a bulk insert operation returning the inserted/updated entities, from the DbSet.
78112 /// </summary>
79- public static List < T > ExecuteBulkInsertReturnEntities < T > (
113+ public static IAsyncEnumerable < T > ExecuteBulkInsertReturnEnumerableAsync < T > (
80114 this DbSet < T > dbSet ,
81115 IEnumerable < T > entities ,
82116 Action < BulkInsertOptions > ? configure = null ,
83- OnConflictOptions < T > ? onConflict = null
117+ OnConflictOptions < T > ? onConflict = null ,
118+ CancellationToken ctk = default
84119 ) where T : class
85120 {
86121 var provider = InitProvider ( dbSet , configure , out var context , out var options ) ;
87- var tableInfo = dbSet . GetDbContext ( ) . GetTableInfo < T > ( ) ;
88122
89- return provider . BulkInsertReturnEntities ( true , context , tableInfo , entities , options , onConflict ) . GetAwaiter ( ) . GetResult ( ) ;
123+ return provider . BulkInsertReturnEntities ( false , context , dbSet . GetDbContext ( ) . GetTableInfo < T > ( ) , entities , options , onConflict , ctk ) ;
90124 }
91125
92126 /// <summary>
93- /// Executes a bulk insert operation returning the inserted/updated entities, from the DbContext (synchronous variant) .
127+ /// Executes a bulk insert operation without returning the inserted/updated entities, from the DbContext.
94128 /// </summary>
95- public static List < T > ExecuteBulkInsertReturnEntities < T > (
129+ public static async Task ExecuteBulkInsertAsync < T > (
96130 this DbContext dbContext ,
97131 IEnumerable < T > entities ,
98132 Action < BulkInsertOptions > ? configure = null ,
99- OnConflictOptions < T > ? onConflict = null
133+ OnConflictOptions < T > ? onConflict = null ,
134+ CancellationToken ctk = default
100135 ) where T : class
101136 {
102- var dbSet = dbContext . Set < T > ( ) ;
103- if ( dbSet == null )
104- {
105- throw new InvalidOperationException ( $ "DbSet of type { typeof ( T ) . Name } not found in DbContext.") ;
106- }
137+ var dbSet = dbContext . Set < T > ( ) ?? throw new InvalidOperationException ( $ "DbSet of type { typeof ( T ) . Name } not found in DbContext.") ;
107138
108- return dbSet . ExecuteBulkInsertReturnEntities ( entities , configure , onConflict ) ;
139+ await dbSet . ExecuteBulkInsertAsync ( entities , configure , onConflict , ctk ) ;
109140 }
110141
111142 /// <summary>
112- /// Executes a bulk insert operation without returning the inserted/updated entities, from the DbSet (synchronous variant) .
143+ /// Executes a bulk insert operation without returning the inserted/updated entities, from the DbSet.
113144 /// </summary>
114- public static void ExecuteBulkInsert < T > (
145+ public static async Task ExecuteBulkInsertAsync < T > (
115146 this DbSet < T > dbSet ,
116147 IEnumerable < T > entities ,
117148 Action < BulkInsertOptions > ? configure = null ,
118- OnConflictOptions < T > ? onConflict = null
149+ OnConflictOptions < T > ? onConflict = null ,
150+ CancellationToken ctk = default
119151 ) where T : class
120152 {
121153 var provider = InitProvider ( dbSet , configure , out var context , out var options ) ;
122- var tableInfo = dbSet . GetDbContext ( ) . GetTableInfo < T > ( ) ;
123154
124- provider . BulkInsert ( true , context , tableInfo , entities , options , onConflict ) . GetAwaiter ( ) . GetResult ( ) ;
155+ await provider . BulkInsert ( false , context , dbSet . GetDbContext ( ) . GetTableInfo < T > ( ) , entities , options , onConflict , ctk ) ;
125156 }
126157
127158 /// <summary>
@@ -134,15 +165,26 @@ public static void ExecuteBulkInsert<T>(
134165 OnConflictOptions < T > ? onConflict = null
135166 ) where T : class
136167 {
137- var dbSet = dbContext . Set < T > ( ) ;
138- if ( dbSet == null )
139- {
140- throw new InvalidOperationException ( $ "DbSet of type { typeof ( T ) . Name } not found in DbContext.") ;
141- }
168+ var dbSet = dbContext . Set < T > ( ) ?? throw new InvalidOperationException ( $ "DbSet of type { typeof ( T ) . Name } not found in DbContext.") ;
142169
143170 dbSet . ExecuteBulkInsert ( entities , configure , onConflict ) ;
144171 }
145172
173+ /// <summary>
174+ /// Executes a bulk insert operation without returning the inserted/updated entities, from the DbSet (synchronous variant).
175+ /// </summary>
176+ public static void ExecuteBulkInsert < T > (
177+ this DbSet < T > dbSet ,
178+ IEnumerable < T > entities ,
179+ Action < BulkInsertOptions > ? configure = null ,
180+ OnConflictOptions < T > ? onConflict = null
181+ ) where T : class
182+ {
183+ var provider = InitProvider ( dbSet , configure , out var context , out var options ) ;
184+
185+ provider . BulkInsert ( true , context , dbSet . GetDbContext ( ) . GetTableInfo < T > ( ) , entities , options , onConflict ) . GetAwaiter ( ) . GetResult ( ) ;
186+ }
187+
146188 private static DbContext GetDbContext < T > ( this DbSet < T > dbSet ) where T : class
147189 {
148190 IInfrastructure < IServiceProvider > infrastructure = dbSet ;
0 commit comments