@@ -12,97 +12,110 @@ internal static class DbContextBulkTransaction
12
12
{
13
13
public static void Execute < T > ( DbContext context , Type ? type , IEnumerable < T > entities , OperationType operationType , BulkConfig ? bulkConfig , Action < decimal > ? progress ) where T : class
14
14
{
15
- SqlAdaptersMapping . ProviderName = context . Database . ProviderName ;
15
+ UpdateSqlAdaptersProps ( context ) ;
16
16
17
17
type ??= typeof ( T ) ;
18
18
19
19
using ( ActivitySources . StartExecuteActivity ( operationType , entities . Count ( ) ) )
20
20
{
21
- if ( entities . Count ( ) == 0 &&
22
- operationType != OperationType . InsertOrUpdateOrDelete &&
23
- operationType != OperationType . Truncate &&
24
- operationType != OperationType . SaveChanges &&
25
- ( bulkConfig == null || bulkConfig . CustomSourceTableName == null ) &&
26
- ( bulkConfig == null || bulkConfig . DataReader == null ) )
27
- {
28
- return ;
29
- }
21
+ if ( ! IsValidTransaction ( entities , operationType , bulkConfig ) ) return ;
30
22
31
23
if ( operationType == OperationType . SaveChanges )
32
24
{
33
25
DbContextBulkTransactionSaveChanges . SaveChanges ( context , bulkConfig , progress ) ;
34
26
return ;
35
27
}
36
- else if ( bulkConfig ? . IncludeGraph == true )
28
+
29
+ if ( bulkConfig ? . IncludeGraph == true )
37
30
{
38
31
DbContextBulkTransactionGraphUtil . ExecuteWithGraph ( context , entities , operationType , bulkConfig , progress ) ;
32
+ return ;
39
33
}
40
- else
41
- {
42
- TableInfo tableInfo = TableInfo . CreateInstance ( context , type , entities , operationType , bulkConfig ) ;
43
34
44
- if ( operationType == OperationType . Insert && ! tableInfo . BulkConfig . SetOutputIdentity && tableInfo . BulkConfig . CustomSourceTableName == null )
45
- {
35
+ var tableInfo = TableInfo . CreateInstance ( context , type , entities , operationType , bulkConfig ) ;
36
+
37
+ switch ( operationType )
38
+ {
39
+ case OperationType . Insert when tableInfo . BulkConfig is { SetOutputIdentity : false , CustomSourceTableName : null } :
46
40
SqlBulkOperation . Insert ( context , type , entities , tableInfo , progress ) ;
47
- }
48
- else if ( operationType == OperationType . Read )
49
- {
41
+ break ;
42
+
43
+ case OperationType . Read :
50
44
SqlBulkOperation . Read ( context , type , entities , tableInfo , progress ) ;
51
- }
52
- else if ( operationType == OperationType . Truncate )
53
- {
45
+ break ;
46
+
47
+ case OperationType . Truncate :
54
48
SqlBulkOperation . Truncate ( context , tableInfo ) ;
55
- }
56
- else
57
- {
49
+ break ;
50
+
51
+ default :
58
52
SqlBulkOperation . Merge ( context , type , entities , tableInfo , operationType , progress ) ;
59
- }
53
+ break ;
60
54
}
61
55
}
62
56
}
63
57
64
58
public static async Task ExecuteAsync < T > ( DbContext context , Type ? type , IEnumerable < T > entities , OperationType operationType , BulkConfig ? bulkConfig , Action < decimal > ? progress , CancellationToken cancellationToken = default ) where T : class
65
59
{
66
- SqlAdaptersMapping . ProviderName = context . Database . ProviderName ;
60
+ UpdateSqlAdaptersProps ( context ) ;
67
61
68
62
type ??= typeof ( T ) ;
69
63
70
64
using ( ActivitySources . StartExecuteActivity ( operationType , entities . Count ( ) ) )
71
65
{
72
- if ( entities . Count ( ) == 0 && operationType != OperationType . InsertOrUpdateOrDelete && operationType != OperationType . Truncate && operationType != OperationType . SaveChanges )
73
- {
74
- return ;
75
- }
66
+ if ( ! IsValidTransaction ( entities , operationType , bulkConfig ) ) return ;
76
67
77
68
if ( operationType == OperationType . SaveChanges )
78
69
{
79
70
await DbContextBulkTransactionSaveChanges . SaveChangesAsync ( context , bulkConfig , progress , cancellationToken ) . ConfigureAwait ( false ) ;
71
+ return ;
80
72
}
81
- else if ( bulkConfig ? . IncludeGraph == true )
73
+
74
+ if ( bulkConfig ? . IncludeGraph == true )
82
75
{
83
76
await DbContextBulkTransactionGraphUtil . ExecuteWithGraphAsync ( context , entities , operationType , bulkConfig , progress , cancellationToken ) . ConfigureAwait ( false ) ;
77
+ return ;
84
78
}
85
- else
86
- {
87
- TableInfo tableInfo = TableInfo . CreateInstance ( context , type , entities , operationType , bulkConfig ) ;
88
79
89
- if ( operationType == OperationType . Insert && ! tableInfo . BulkConfig . SetOutputIdentity )
90
- {
80
+ var tableInfo = TableInfo . CreateInstance ( context , type , entities , operationType , bulkConfig ) ;
81
+
82
+ switch ( operationType )
83
+ {
84
+ case OperationType . Insert when ! tableInfo . BulkConfig . SetOutputIdentity :
91
85
await SqlBulkOperation . InsertAsync ( context , type , entities , tableInfo , progress , cancellationToken ) . ConfigureAwait ( false ) ;
92
- }
93
- else if ( operationType == OperationType . Read )
94
- {
86
+ break ;
87
+
88
+ case OperationType . Read :
95
89
await SqlBulkOperation . ReadAsync ( context , type , entities , tableInfo , progress , cancellationToken ) . ConfigureAwait ( false ) ;
96
- }
97
- else if ( operationType == OperationType . Truncate )
98
- {
90
+ break ;
91
+
92
+ case OperationType . Truncate :
99
93
await SqlBulkOperation . TruncateAsync ( context , tableInfo , cancellationToken ) . ConfigureAwait ( false ) ;
100
- }
101
- else
102
- {
94
+ break ;
95
+
96
+ default :
103
97
await SqlBulkOperation . MergeAsync ( context , type , entities , tableInfo , operationType , progress , cancellationToken ) . ConfigureAwait ( false ) ;
104
- }
98
+ break ;
105
99
}
106
100
}
107
101
}
102
+
103
+ #region SqlAdapters Settings
104
+ private static void UpdateSqlAdaptersProps ( DbContext context )
105
+ {
106
+ SqlAdaptersMapping . ProviderName = context . Database . ProviderName ;
107
+ }
108
+ #endregion
109
+
110
+ #region Transaction Validators
111
+ private static bool IsValidTransaction < T > ( IEnumerable < T > entities , OperationType operationType , BulkConfig ? bulkConfig )
112
+ {
113
+ return entities . Any ( ) ||
114
+ operationType == OperationType . Truncate ||
115
+ operationType == OperationType . SaveChanges ||
116
+ operationType == OperationType . InsertOrUpdateOrDelete ||
117
+ bulkConfig is { CustomSourceTableName : not null } ||
118
+ bulkConfig is { DataReader : not null } ;
119
+ }
120
+ #endregion
108
121
}
0 commit comments