Skip to content

Commit 93bfd2c

Browse files
committed
Cache
1 parent d2088ea commit 93bfd2c

18 files changed

+157
-236
lines changed

src/EntityFrameworkCore.SqlServer.SimpleBulks/BulkDelete/DbContextAsyncExtensions.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.EntityFrameworkCore;
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65
using System.Threading;
76
using System.Threading.Tasks;
87

@@ -15,15 +14,11 @@ public static Task<BulkDeleteResult> BulkDeleteAsync<T>(this DbContext dbContext
1514
var table = dbContext.GetTableInfor(typeof(T));
1615
var connection = dbContext.GetSqlConnection();
1716
var transaction = dbContext.GetCurrentSqlTransaction();
18-
var properties = dbContext.GetProperties(typeof(T));
19-
var primaryKeys = properties
20-
.Where(x => x.IsPrimaryKey)
21-
.Select(x => x.PropertyName);
2217

2318
return new BulkDeleteBuilder<T>(connection, transaction)
24-
.WithId(primaryKeys)
25-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
26-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
19+
.WithId(dbContext.GetPrimaryKeys(typeof(T)))
20+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
21+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
2722
.ToTable(table)
2823
.ConfigureBulkOptions(configureOptions)
2924
.ExecuteAsync(data, cancellationToken);

src/EntityFrameworkCore.SqlServer.SimpleBulks/BulkDelete/DbContextExtensions.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.EntityFrameworkCore;
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65

76
namespace EntityFrameworkCore.SqlServer.SimpleBulks.BulkDelete;
87

@@ -13,15 +12,11 @@ public static BulkDeleteResult BulkDelete<T>(this DbContext dbContext, IEnumerab
1312
var table = dbContext.GetTableInfor(typeof(T));
1413
var connection = dbContext.GetSqlConnection();
1514
var transaction = dbContext.GetCurrentSqlTransaction();
16-
var properties = dbContext.GetProperties(typeof(T));
17-
var primaryKeys = properties
18-
.Where(x => x.IsPrimaryKey)
19-
.Select(x => x.PropertyName);
2015

2116
return new BulkDeleteBuilder<T>(connection, transaction)
22-
.WithId(primaryKeys)
23-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
24-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
17+
.WithId(dbContext.GetPrimaryKeys(typeof(T)))
18+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
19+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
2520
.ToTable(table)
2621
.ConfigureBulkOptions(configureOptions)
2722
.Execute(data);

src/EntityFrameworkCore.SqlServer.SimpleBulks/BulkInsert/DbContextAsyncExtensions.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using EntityFrameworkCore.SqlServer.SimpleBulks.Extensions;
22
using Microsoft.EntityFrameworkCore;
3-
using Microsoft.EntityFrameworkCore.Metadata;
43
using System;
54
using System.Collections.Generic;
6-
using System.Linq;
75
using System.Linq.Expressions;
86
using System.Threading;
97
using System.Threading.Tasks;
@@ -17,18 +15,12 @@ public static Task BulkInsertAsync<T>(this DbContext dbContext, IEnumerable<T> d
1715
var table = dbContext.GetTableInfor(typeof(T));
1816
var connection = dbContext.GetSqlConnection();
1917
var transaction = dbContext.GetCurrentSqlTransaction();
20-
var properties = dbContext.GetProperties(typeof(T));
21-
var columns = properties
22-
.Where(x => x.ValueGenerated == ValueGenerated.Never)
23-
.Select(x => x.PropertyName);
24-
var idColumn = properties
25-
.Where(x => x.IsPrimaryKey && x.ValueGenerated == ValueGenerated.OnAdd)
26-
.FirstOrDefault();
18+
var idColumn = dbContext.GetOutputId(typeof(T));
2719

2820
return new BulkInsertBuilder<T>(connection, transaction)
29-
.WithColumns(columns)
30-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
31-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
21+
.WithColumns(dbContext.GetInsertablePropertyNames(typeof(T)))
22+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
23+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
3224
.ToTable(table)
3325
.WithOutputId(idColumn?.PropertyName)
3426
.WithOutputIdMode(GetOutputIdMode(idColumn))
@@ -41,15 +33,12 @@ public static Task BulkInsertAsync<T>(this DbContext dbContext, IEnumerable<T> d
4133
var table = dbContext.GetTableInfor(typeof(T));
4234
var connection = dbContext.GetSqlConnection();
4335
var transaction = dbContext.GetCurrentSqlTransaction();
44-
var properties = dbContext.GetProperties(typeof(T));
45-
var idColumn = properties
46-
.Where(x => x.IsPrimaryKey && x.ValueGenerated == ValueGenerated.OnAdd)
47-
.FirstOrDefault();
36+
var idColumn = dbContext.GetOutputId(typeof(T));
4837

4938
return new BulkInsertBuilder<T>(connection, transaction)
5039
.WithColumns(columnNamesSelector)
51-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
52-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
40+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
41+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
5342
.ToTable(table)
5443
.WithOutputId(idColumn?.PropertyName)
5544
.WithOutputIdMode(GetOutputIdMode(idColumn))

src/EntityFrameworkCore.SqlServer.SimpleBulks/BulkInsert/DbContextExtensions.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using EntityFrameworkCore.SqlServer.SimpleBulks.Extensions;
22
using Microsoft.EntityFrameworkCore;
3-
using Microsoft.EntityFrameworkCore.Metadata;
43
using System;
54
using System.Collections.Generic;
6-
using System.Linq;
75
using System.Linq.Expressions;
86

97
namespace EntityFrameworkCore.SqlServer.SimpleBulks.BulkInsert;
@@ -15,18 +13,12 @@ public static void BulkInsert<T>(this DbContext dbContext, IEnumerable<T> data,
1513
var table = dbContext.GetTableInfor(typeof(T));
1614
var connection = dbContext.GetSqlConnection();
1715
var transaction = dbContext.GetCurrentSqlTransaction();
18-
var properties = dbContext.GetProperties(typeof(T));
19-
var columns = properties
20-
.Where(x => x.ValueGenerated == ValueGenerated.Never)
21-
.Select(x => x.PropertyName);
22-
var idColumn = properties
23-
.Where(x => x.IsPrimaryKey && x.ValueGenerated == ValueGenerated.OnAdd)
24-
.FirstOrDefault();
16+
var idColumn = dbContext.GetOutputId(typeof(T));
2517

2618
new BulkInsertBuilder<T>(connection, transaction)
27-
.WithColumns(columns)
28-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
29-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
19+
.WithColumns(dbContext.GetInsertablePropertyNames(typeof(T)))
20+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
21+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
3022
.ToTable(table)
3123
.WithOutputId(idColumn?.PropertyName)
3224
.WithOutputIdMode(GetOutputIdMode(idColumn))
@@ -39,15 +31,12 @@ public static void BulkInsert<T>(this DbContext dbContext, IEnumerable<T> data,
3931
var table = dbContext.GetTableInfor(typeof(T));
4032
var connection = dbContext.GetSqlConnection();
4133
var transaction = dbContext.GetCurrentSqlTransaction();
42-
var properties = dbContext.GetProperties(typeof(T));
43-
var idColumn = properties
44-
.Where(x => x.IsPrimaryKey && x.ValueGenerated == ValueGenerated.OnAdd)
45-
.FirstOrDefault();
34+
var idColumn = dbContext.GetOutputId(typeof(T));
4635

4736
new BulkInsertBuilder<T>(connection, transaction)
4837
.WithColumns(columnNamesSelector)
49-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
50-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
38+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
39+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
5140
.ToTable(table)
5241
.WithOutputId(idColumn?.PropertyName)
5342
.WithOutputIdMode(GetOutputIdMode(idColumn))

src/EntityFrameworkCore.SqlServer.SimpleBulks/BulkMatch/DbContextAsyncExtensions.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.EntityFrameworkCore;
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65
using System.Linq.Expressions;
76
using System.Threading;
87
using System.Threading.Tasks;
@@ -16,13 +15,11 @@ public static Task<List<T>> BulkMatchAsync<T>(this DbContext dbContext, IEnumera
1615
var table = dbContext.GetTableInfor(typeof(T));
1716
var connection = dbContext.GetSqlConnection();
1817
var transaction = dbContext.GetCurrentSqlTransaction();
19-
var properties = dbContext.GetProperties(typeof(T));
20-
var columns = properties.Select(x => x.PropertyName);
2118

2219
return new BulkMatchBuilder<T>(connection, transaction)
23-
.WithReturnedColumns(columns)
24-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
25-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
20+
.WithReturnedColumns(dbContext.GetAllPropertyNames(typeof(T)))
21+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
22+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
2623
.WithTable(table)
2724
.WithMatchedColumns(matchedColumnsSelector)
2825
.ConfigureBulkOptions(configureOptions)
@@ -34,12 +31,11 @@ public static Task<List<T>> BulkMatchAsync<T>(this DbContext dbContext, IEnumera
3431
var table = dbContext.GetTableInfor(typeof(T));
3532
var connection = dbContext.GetSqlConnection();
3633
var transaction = dbContext.GetCurrentSqlTransaction();
37-
var properties = dbContext.GetProperties(typeof(T));
3834

3935
return new BulkMatchBuilder<T>(connection, transaction)
4036
.WithReturnedColumns(returnedColumnsSelector)
41-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
42-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
37+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
38+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
4339
.WithTable(table)
4440
.WithMatchedColumns(matchedColumnsSelector)
4541
.ConfigureBulkOptions(configureOptions)

src/EntityFrameworkCore.SqlServer.SimpleBulks/BulkMatch/DbContextExtensions.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.EntityFrameworkCore;
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65
using System.Linq.Expressions;
76

87
namespace EntityFrameworkCore.SqlServer.SimpleBulks.BulkMatch;
@@ -14,13 +13,11 @@ public static List<T> BulkMatch<T>(this DbContext dbContext, IEnumerable<T> mach
1413
var table = dbContext.GetTableInfor(typeof(T));
1514
var connection = dbContext.GetSqlConnection();
1615
var transaction = dbContext.GetCurrentSqlTransaction();
17-
var properties = dbContext.GetProperties(typeof(T));
18-
var columns = properties.Select(x => x.PropertyName);
1916

2017
return new BulkMatchBuilder<T>(connection, transaction)
21-
.WithReturnedColumns(columns)
22-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
23-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
18+
.WithReturnedColumns(dbContext.GetAllPropertyNames(typeof(T)))
19+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
20+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
2421
.WithTable(table)
2522
.WithMatchedColumns(matchedColumnsSelector)
2623
.ConfigureBulkOptions(configureOptions)
@@ -32,12 +29,11 @@ public static List<T> BulkMatch<T>(this DbContext dbContext, IEnumerable<T> mach
3229
var table = dbContext.GetTableInfor(typeof(T));
3330
var connection = dbContext.GetSqlConnection();
3431
var transaction = dbContext.GetCurrentSqlTransaction();
35-
var properties = dbContext.GetProperties(typeof(T));
3632

3733
return new BulkMatchBuilder<T>(connection, transaction)
3834
.WithReturnedColumns(returnedColumnsSelector)
39-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
40-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
35+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
36+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
4137
.WithTable(table)
4238
.WithMatchedColumns(matchedColumnsSelector)
4339
.ConfigureBulkOptions(configureOptions)

src/EntityFrameworkCore.SqlServer.SimpleBulks/BulkMerge/DbContextAsyncExtensions.cs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using EntityFrameworkCore.SqlServer.SimpleBulks.Extensions;
22
using Microsoft.EntityFrameworkCore;
3-
using Microsoft.EntityFrameworkCore.Metadata;
43
using System;
54
using System.Collections.Generic;
6-
using System.Linq;
75
using System.Linq.Expressions;
86
using System.Threading;
97
using System.Threading.Tasks;
@@ -17,18 +15,14 @@ public static Task<BulkMergeResult> BulkMergeAsync<T>(this DbContext dbContext,
1715
var table = dbContext.GetTableInfor(typeof(T));
1816
var connection = dbContext.GetSqlConnection();
1917
var transaction = dbContext.GetCurrentSqlTransaction();
20-
var properties = dbContext.GetProperties(typeof(T));
21-
var outputIdColumn = properties
22-
.Where(x => x.IsPrimaryKey && x.ValueGenerated == ValueGenerated.OnAdd)
23-
.Select(x => x.PropertyName)
24-
.FirstOrDefault();
18+
var outputIdColumn = dbContext.GetOutputId(typeof(T))?.PropertyName;
2519

2620
return new BulkMergeBuilder<T>(connection, transaction)
2721
.WithId(idSelector)
2822
.WithUpdateColumns(updateColumnNamesSelector)
2923
.WithInsertColumns(insertColumnNamesSelector)
30-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
31-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
24+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
25+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
3226
.WithOutputId(outputIdColumn)
3327
.ToTable(table)
3428
.ConfigureBulkOptions(configureOptions)
@@ -40,18 +34,14 @@ public static Task<BulkMergeResult> BulkMergeAsync<T>(this DbContext dbContext,
4034
var table = dbContext.GetTableInfor(typeof(T));
4135
var connection = dbContext.GetSqlConnection();
4236
var transaction = dbContext.GetCurrentSqlTransaction();
43-
var properties = dbContext.GetProperties(typeof(T));
44-
var outputIdColumn = properties
45-
.Where(x => x.IsPrimaryKey && x.ValueGenerated == ValueGenerated.OnAdd)
46-
.Select(x => x.PropertyName)
47-
.FirstOrDefault();
37+
var outputIdColumn = dbContext.GetOutputId(typeof(T))?.PropertyName;
4838

4939
return new BulkMergeBuilder<T>(connection, transaction)
5040
.WithId(idColumn)
5141
.WithUpdateColumns(updateColumnNames)
5242
.WithInsertColumns(insertColumnNames)
53-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
54-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
43+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
44+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
5545
.WithOutputId(outputIdColumn)
5646
.ToTable(table)
5747
.ConfigureBulkOptions(configureOptions)
@@ -63,18 +53,14 @@ public static Task<BulkMergeResult> BulkMergeAsync<T>(this DbContext dbContext,
6353
var table = dbContext.GetTableInfor(typeof(T));
6454
var connection = dbContext.GetSqlConnection();
6555
var transaction = dbContext.GetCurrentSqlTransaction();
66-
var properties = dbContext.GetProperties(typeof(T));
67-
var outputIdColumn = properties
68-
.Where(x => x.IsPrimaryKey && x.ValueGenerated == ValueGenerated.OnAdd)
69-
.Select(x => x.PropertyName)
70-
.FirstOrDefault();
56+
var outputIdColumn = dbContext.GetOutputId(typeof(T))?.PropertyName;
7157

7258
return new BulkMergeBuilder<T>(connection, transaction)
7359
.WithId(idColumns)
7460
.WithUpdateColumns(updateColumnNames)
7561
.WithInsertColumns(insertColumnNames)
76-
.WithDbColumnMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnName))
77-
.WithDbColumnTypeMappings(properties.ToDictionary(x => x.PropertyName, x => x.ColumnType))
62+
.WithDbColumnMappings(dbContext.GetColumnNames(typeof(T)))
63+
.WithDbColumnTypeMappings(dbContext.GetColumnTypes(typeof(T)))
7864
.WithOutputId(outputIdColumn)
7965
.ToTable(table)
8066
.ConfigureBulkOptions(configureOptions)

0 commit comments

Comments
 (0)