11using System ;
22using System . Collections . Generic ;
3-
3+ using System . Threading ;
4+ using System . Threading . Tasks ;
45using Microsoft . EntityFrameworkCore ;
56
67using JetBrains . Annotations ;
@@ -22,7 +23,7 @@ public static partial class LinqToDBForEFTools
2223 /// <param name="options">Operation options.</param>
2324 /// <param name="source">Records to insert.</param>
2425 /// <returns>Bulk insert operation status.</returns>
25- public static BulkCopyRowsCopied BulkCopy < T > ( [ NotNull ] this DbContext context , BulkCopyOptions options , IEnumerable < T > source ) where T : class
26+ public static BulkCopyRowsCopied BulkCopy < T > ( [ JetBrains . Annotations . NotNull ] this DbContext context , BulkCopyOptions options , IEnumerable < T > source ) where T : class
2627 {
2728 if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
2829
@@ -40,7 +41,7 @@ public static BulkCopyRowsCopied BulkCopy<T>([NotNull] this DbContext context, B
4041 /// <param name="maxBatchSize">Number of rows in each batch. At the end of each batch, the rows in the batch are sent to the server. </param>
4142 /// <param name="source">Records to insert.</param>
4243 /// <returns>Bulk insert operation status.</returns>
43- public static BulkCopyRowsCopied BulkCopy < T > ( [ NotNull ] this DbContext context , int maxBatchSize , IEnumerable < T > source ) where T : class
44+ public static BulkCopyRowsCopied BulkCopy < T > ( [ JetBrains . Annotations . NotNull ] this DbContext context , int maxBatchSize , IEnumerable < T > source ) where T : class
4445 {
4546 if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
4647
@@ -59,7 +60,7 @@ public static BulkCopyRowsCopied BulkCopy<T>([NotNull] this DbContext context, i
5960 /// <param name="context">Database context.</param>
6061 /// <param name="source">Records to insert.</param>
6162 /// <returns>Bulk insert operation status.</returns>
62- public static BulkCopyRowsCopied BulkCopy < T > ( [ NotNull ] this DbContext context , IEnumerable < T > source ) where T : class
63+ public static BulkCopyRowsCopied BulkCopy < T > ( [ JetBrains . Annotations . NotNull ] this DbContext context , IEnumerable < T > source ) where T : class
6364 {
6465 if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
6566
@@ -73,6 +74,153 @@ public static BulkCopyRowsCopied BulkCopy<T>([NotNull] this DbContext context, I
7374
7475 #endregion
7576
77+ #region BulkCopyAsync
78+
79+ /// <summary>Asynchronously performs bulk insert operation.</summary>
80+ /// <typeparam name="T">Mapping type of inserted record.</typeparam>
81+ /// <param name="context">Database context.</param>
82+ /// <param name="options">Operation options.</param>
83+ /// <param name="source">Records to insert.</param>
84+ /// <param name="cancellationToken">Asynchronous operation cancellation token.</param>
85+ /// <returns>Task with bulk insert operation status.</returns>
86+ public static Task < BulkCopyRowsCopied > BulkCopyAsync < T > (
87+ [ JetBrains . Annotations . NotNull ] this DbContext context ,
88+ BulkCopyOptions options ,
89+ IEnumerable < T > source ,
90+ CancellationToken cancellationToken = default )
91+ where T : class
92+ {
93+ if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
94+ if ( source == null ) throw new ArgumentNullException ( nameof ( source ) ) ;
95+
96+ using ( var dc = context . CreateLinqToDbConnection ( ) )
97+ {
98+ return dc . BulkCopyAsync ( options , source , cancellationToken ) ;
99+ }
100+ }
101+
102+ /// <summary>Asynchronously performs bulk insert operation.</summary>
103+ /// <typeparam name="T">Mapping type of inserted record.</typeparam>
104+ /// <param name="context">Database context.</param>
105+ /// <param name="maxBatchSize">
106+ /// Number of rows in each batch. At the end of each batch, the rows in the batch are sent to
107+ /// the server.
108+ /// </param>
109+ /// <param name="source">Records to insert.</param>
110+ /// <param name="cancellationToken">Asynchronous operation cancellation token.</param>
111+ /// <returns>Task with bulk insert operation status.</returns>
112+ public static Task < BulkCopyRowsCopied > BulkCopyAsync < T > (
113+ [ JetBrains . Annotations . NotNull ] this DbContext context ,
114+ int maxBatchSize ,
115+ IEnumerable < T > source ,
116+ CancellationToken cancellationToken = default )
117+ where T : class
118+ {
119+ if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
120+ if ( source == null ) throw new ArgumentNullException ( nameof ( source ) ) ;
121+
122+ if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
123+ if ( source == null ) throw new ArgumentNullException ( nameof ( source ) ) ;
124+
125+ using ( var dc = context . CreateLinqToDbConnection ( ) )
126+ {
127+ return dc . BulkCopyAsync ( maxBatchSize , source , cancellationToken ) ;
128+ }
129+ }
130+
131+ /// <summary>Asynchronously performs bulk insert operation.</summary>
132+ /// <typeparam name="T">Mapping type of inserted record.</typeparam>
133+ /// <param name="context">Database context.</param>
134+ /// <param name="source">Records to insert.</param>
135+ /// <param name="cancellationToken">Asynchronous operation cancellation token.</param>
136+ /// <returns>Task with bulk insert operation status.</returns>
137+ public static Task < BulkCopyRowsCopied > BulkCopyAsync < T > (
138+ [ JetBrains . Annotations . NotNull ] this DbContext context ,
139+ IEnumerable < T > source ,
140+ CancellationToken cancellationToken = default )
141+ where T : class
142+ {
143+ if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
144+ if ( source == null ) throw new ArgumentNullException ( nameof ( source ) ) ;
145+
146+ using ( var dc = context . CreateLinqToDbConnection ( ) )
147+ {
148+ return dc . BulkCopyAsync ( source , cancellationToken ) ;
149+ }
150+ }
151+
152+ /// <summary>Asynchronously performs bulk insert operation.</summary>
153+ /// <typeparam name="T">Mapping type of inserted record.</typeparam>
154+ /// <param name="context">Database context.</param>
155+ /// <param name="options">Operation options.</param>
156+ /// <param name="source">Records to insert.</param>
157+ /// <param name="cancellationToken">Asynchronous operation cancellation token.</param>
158+ /// <returns>Task with bulk insert operation status.</returns>
159+ public static Task < BulkCopyRowsCopied > BulkCopyAsync < T > (
160+ [ JetBrains . Annotations . NotNull ] this DbContext context ,
161+ BulkCopyOptions options ,
162+ IAsyncEnumerable < T > source ,
163+ CancellationToken cancellationToken = default )
164+ where T : class
165+ {
166+ if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
167+ if ( source == null ) throw new ArgumentNullException ( nameof ( source ) ) ;
168+
169+ using ( var dc = context . CreateLinqToDbConnection ( ) )
170+ {
171+ return dc . BulkCopyAsync ( options , source , cancellationToken ) ;
172+ }
173+ }
174+
175+ /// <summary>Asynchronously performs bulk insert operation.</summary>
176+ /// <typeparam name="T">Mapping type of inserted record.</typeparam>
177+ /// <param name="context">Database context.</param>
178+ /// <param name="maxBatchSize">
179+ /// Number of rows in each batch. At the end of each batch, the rows in the batch are sent to
180+ /// the server.
181+ /// </param>
182+ /// <param name="source">Records to insert.</param>
183+ /// <param name="cancellationToken">Asynchronous operation cancellation token.</param>
184+ /// <returns>Task with bulk insert operation status.</returns>
185+ public static Task < BulkCopyRowsCopied > BulkCopyAsync < T > (
186+ [ JetBrains . Annotations . NotNull ] this DbContext context ,
187+ int maxBatchSize ,
188+ IAsyncEnumerable < T > source ,
189+ CancellationToken cancellationToken = default )
190+ where T : class
191+ {
192+ if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
193+ if ( source == null ) throw new ArgumentNullException ( nameof ( source ) ) ;
194+
195+ using ( var dc = context . CreateLinqToDbConnection ( ) )
196+ {
197+ return dc . BulkCopyAsync ( maxBatchSize , source , cancellationToken ) ;
198+ }
199+ }
200+
201+ /// <summary>Asynchronously performs bulk insert operation.</summary>
202+ /// <typeparam name="T">Mapping type of inserted record.</typeparam>
203+ /// <param name="context">Database context.</param>
204+ /// <param name="source">Records to insert.</param>
205+ /// <param name="cancellationToken">Asynchronous operation cancellation token.</param>
206+ /// <returns>Task with bulk insert operation status.</returns>
207+ public static Task < BulkCopyRowsCopied > BulkCopyAsync < T > (
208+ [ JetBrains . Annotations . NotNull ] this DbContext context ,
209+ IAsyncEnumerable < T > source ,
210+ CancellationToken cancellationToken = default )
211+ where T : class
212+ {
213+ if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
214+ if ( source == null ) throw new ArgumentNullException ( nameof ( source ) ) ;
215+
216+ using ( var dc = context . CreateLinqToDbConnection ( ) )
217+ {
218+ return dc . BulkCopyAsync ( source , cancellationToken ) ;
219+ }
220+ }
221+
222+ #endregion
223+
76224 #region Value Insertable
77225
78226 /// <summary>
@@ -84,7 +232,7 @@ public static BulkCopyRowsCopied BulkCopy<T>([NotNull] this DbContext context, I
84232 /// <returns>Insertable source query.</returns>
85233 [ LinqTunnel ]
86234 [ Pure ]
87- public static IValueInsertable < T > Into < T > ( [ NotNull ] this DbContext context , [ NotNull ] ITable < T > target )
235+ public static IValueInsertable < T > Into < T > ( [ JetBrains . Annotations . NotNull ] this DbContext context , [ JetBrains . Annotations . NotNull ] ITable < T > target )
88236 {
89237 if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
90238 if ( target == null ) throw new ArgumentNullException ( nameof ( target ) ) ;
@@ -101,7 +249,7 @@ public static IValueInsertable<T> Into<T>([NotNull] this DbContext context, [Not
101249 /// </summary>
102250 /// <typeparam name="T">Mapping class type.</typeparam>
103251 /// <returns>Queryable source.</returns>
104- public static ITable < T > GetTable < T > ( [ NotNull ] this DbContext context )
252+ public static ITable < T > GetTable < T > ( [ JetBrains . Annotations . NotNull ] this DbContext context )
105253 where T : class
106254 {
107255 if ( context == null ) throw new ArgumentNullException ( nameof ( context ) ) ;
0 commit comments