diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 6c9bef485..197e64800 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -826,5 +826,14 @@ + + + 批量注入 Repository,可以参考代码自行调整 + + + + + + diff --git a/FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlInsertOrUpdateTest.cs b/FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlInsertOrUpdateTest.cs index 91b56e348..bc96af529 100644 --- a/FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlInsertOrUpdateTest.cs +++ b/FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlInsertOrUpdateTest.cs @@ -1,7 +1,8 @@ -using FreeSql.DataAnnotations; +using FreeSql.DataAnnotations; using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Xunit; namespace FreeSql.Tests.MySqlConnector @@ -11,6 +12,31 @@ public class MySqlInsertOrUpdateTest IFreeSql fsql => g.mysql; + [Fact] + public void InsertOrUpdate_ExecuteMySqlBulkCopy_Test() + { + fsql.Delete().Where("1=1").ExecuteAffrows(); + var iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou02 { id = 1, name = "01" }, new tbiou02 { id = 2, name = "02" }, new tbiou02 { id = 3, name = "03" }, new tbiou02 { id = 4, name = "04" } }); + var sql = iou.ToSql(); + Assert.Equal(@"INSERT INTO `tbiou02`(`id`, `name`) VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004') +ON DUPLICATE KEY UPDATE +`name` = VALUES(`name`)", sql); + Assert.Equal(4, iou.ExecuteMySqlBulkCopy()); + } + + [Fact] + public async Task InsertOrUpdate_ExecuteMySqlBulkCopyAsync_Test() + { + fsql.Delete().Where("1=1").ExecuteAffrows(); + var iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou02 { id = 1, name = "01" }, new tbiou02 { id = 2, name = "02" }, new tbiou02 { id = 3, name = "03" }, new tbiou02 { id = 4, name = "04" } }); + var sql = iou.ToSql(); + Assert.Equal(@"INSERT INTO `tbiou02`(`id`, `name`) VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04') +ON DUPLICATE KEY UPDATE +`name` = VALUES(`name`)", sql); + Assert.Equal(4, await iou.ExecuteMySqlBulkCopyAsync()); + } + + [Fact] public void InsertOrUpdate_OnePrimary() { diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index b9fa801f3..f3cb4e047 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -105,6 +105,7 @@ public static int ExecuteBulkCommand(List _source, ColumnInfo[] _tempPri #else public static Task ExecuteBulkUpdateAsync(UpdateProvider update, NativeTuple state, Func, Task> funcBulkCopy) where T1 : class => ExecuteBulkCommandAsync(update._source, update._tempPrimarys, update._orm, update._connection, update._transaction, update._table, state, funcBulkCopy); + public static Task ExecuteBulkUpsertAsync(InsertOrUpdateProvider upsert, NativeTuple state, Func, Task> funcBulkCopy) where T1 : class => ExecuteBulkCommandAsync(upsert._source, upsert._tempPrimarys, upsert._orm, upsert._connection, upsert._transaction, upsert._table, state, funcBulkCopy); diff --git a/Providers/FreeSql.Provider.MySqlConnector/FreeSqlMySqlConnectorGlobalExtensions.cs b/Providers/FreeSql.Provider.MySqlConnector/FreeSqlMySqlConnectorGlobalExtensions.cs index a08d4e237..57a4706fc 100644 --- a/Providers/FreeSql.Provider.MySqlConnector/FreeSqlMySqlConnectorGlobalExtensions.cs +++ b/Providers/FreeSql.Provider.MySqlConnector/FreeSqlMySqlConnectorGlobalExtensions.cs @@ -199,10 +199,10 @@ public static void ExecuteMySqlBulkCopy(this IInsert that, int? bulkCopyTi #else public static Task ExecuteMySqlBulkCopyAsync(this IInsertOrUpdate that, int? bulkCopyTimeout = null, CancellationToken cancellationToken = default) where T : class { - var upsert = that as UpdateProvider; + var upsert = that as InsertOrUpdateProvider; if (upsert._source.Any() != true || upsert._tempPrimarys.Any() == false) return Task.FromResult(0); var state = ExecuteMySqlBulkCopyState(upsert); - return UpdateProvider.ExecuteBulkUpdateAsync(upsert, state, insert => insert.ExecuteMySqlBulkCopyAsync(bulkCopyTimeout, cancellationToken)); + return UpdateProvider.ExecuteBulkUpsertAsync(upsert, state, insert => insert.ExecuteMySqlBulkCopyAsync(bulkCopyTimeout, cancellationToken)); } public static Task ExecuteMySqlBulkCopyAsync(this IUpdate that, int? bulkCopyTimeout = null, CancellationToken cancellationToken = default) where T : class {