-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed BulkUpdate/BulkInsertOrUpdate for entities with auto increment.
- Loading branch information
Showing
13 changed files
with
317 additions
and
121 deletions.
There are no files selected for viewing
135 changes: 70 additions & 65 deletions
135
...rkCore.SqlServer/EntityFrameworkCore/BulkOperations/SqlServerBulkInsertOrUpdateOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,70 @@ | ||
namespace Thinktecture.EntityFrameworkCore.BulkOperations; | ||
|
||
/// <summary> | ||
/// Bulk insert or update options for SQL Server. | ||
/// </summary> | ||
public sealed class SqlServerBulkInsertOrUpdateOptions : ISqlServerMergeOperationOptions, IBulkInsertOrUpdateOptions | ||
{ | ||
/// <inheritdoc /> | ||
public IEntityPropertiesProvider? PropertiesToInsert { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public IEntityPropertiesProvider? PropertiesToUpdate { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public IEntityPropertiesProvider? KeyProperties { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public List<SqlServerTableHintLimited> MergeTableHints { get; } | ||
|
||
/// <inheritdoc /> | ||
public SqlServerBulkOperationTempTableOptions TempTableOptions { get; } | ||
|
||
/// <summary> | ||
/// Initializes new instance of <see cref="SqlServerBulkUpdateOptions"/>. | ||
/// </summary> | ||
/// <param name="optionsToInitializeFrom">Options to initialize from.</param> | ||
public SqlServerBulkInsertOrUpdateOptions(IBulkInsertOrUpdateOptions? optionsToInitializeFrom = null) | ||
{ | ||
if (optionsToInitializeFrom is not null) | ||
{ | ||
PropertiesToInsert = optionsToInitializeFrom.PropertiesToInsert; | ||
PropertiesToUpdate = optionsToInitializeFrom.PropertiesToUpdate; | ||
KeyProperties = optionsToInitializeFrom.KeyProperties; | ||
} | ||
|
||
if (optionsToInitializeFrom is ISqlServerMergeOperationOptions mergeOptions) | ||
{ | ||
TempTableOptions = new SqlServerBulkOperationTempTableOptions(mergeOptions.TempTableOptions); | ||
MergeTableHints = mergeOptions.MergeTableHints.ToList(); | ||
} | ||
else | ||
{ | ||
TempTableOptions = new SqlServerBulkOperationTempTableOptions(); | ||
MergeTableHints = new List<SqlServerTableHintLimited> { SqlServerTableHintLimited.HoldLock }; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the options for bulk insert into a temp table. | ||
/// </summary> | ||
public SqlServerTempTableBulkOperationOptions GetTempTableBulkInsertOptions() | ||
{ | ||
var options = new SqlServerTempTableBulkInsertOptions | ||
{ | ||
PropertiesToInsert = PropertiesToInsert is null || PropertiesToUpdate is null | ||
? null | ||
: CompositeTempTableEntityPropertiesProvider.CreateForInsertOrUpdate(PropertiesToInsert, PropertiesToUpdate, KeyProperties), | ||
Advanced = { UsePropertiesToInsertForTempTableCreation = true } | ||
}; | ||
|
||
TempTableOptions.Populate(options); | ||
|
||
return options; | ||
} | ||
} | ||
using Microsoft.Data.SqlClient; | ||
|
||
namespace Thinktecture.EntityFrameworkCore.BulkOperations; | ||
|
||
/// <summary> | ||
/// Bulk insert or update options for SQL Server. | ||
/// </summary> | ||
public sealed class SqlServerBulkInsertOrUpdateOptions : ISqlServerMergeOperationOptions, IBulkInsertOrUpdateOptions | ||
{ | ||
/// <inheritdoc /> | ||
public IEntityPropertiesProvider? PropertiesToInsert { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public IEntityPropertiesProvider? PropertiesToUpdate { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public IEntityPropertiesProvider? KeyProperties { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public List<SqlServerTableHintLimited> MergeTableHints { get; } | ||
|
||
/// <inheritdoc /> | ||
public SqlServerBulkOperationTempTableOptions TempTableOptions { get; } | ||
|
||
/// <summary> | ||
/// Initializes new instance of <see cref="SqlServerBulkUpdateOptions"/>. | ||
/// </summary> | ||
/// <param name="optionsToInitializeFrom">Options to initialize from.</param> | ||
public SqlServerBulkInsertOrUpdateOptions(IBulkInsertOrUpdateOptions? optionsToInitializeFrom = null) | ||
{ | ||
if (optionsToInitializeFrom is not null) | ||
{ | ||
PropertiesToInsert = optionsToInitializeFrom.PropertiesToInsert; | ||
PropertiesToUpdate = optionsToInitializeFrom.PropertiesToUpdate; | ||
KeyProperties = optionsToInitializeFrom.KeyProperties; | ||
} | ||
|
||
if (optionsToInitializeFrom is ISqlServerMergeOperationOptions mergeOptions) | ||
{ | ||
TempTableOptions = new SqlServerBulkOperationTempTableOptions(mergeOptions.TempTableOptions); | ||
MergeTableHints = mergeOptions.MergeTableHints.ToList(); | ||
} | ||
else | ||
{ | ||
TempTableOptions = new SqlServerBulkOperationTempTableOptions | ||
{ | ||
SqlBulkCopyOptions = SqlBulkCopyOptions.KeepIdentity | ||
}; | ||
MergeTableHints = new List<SqlServerTableHintLimited> { SqlServerTableHintLimited.HoldLock }; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the options for bulk insert into a temp table. | ||
/// </summary> | ||
public SqlServerTempTableBulkOperationOptions GetTempTableBulkInsertOptions() | ||
{ | ||
var options = new SqlServerTempTableBulkInsertOptions | ||
{ | ||
PropertiesToInsert = PropertiesToInsert is null || PropertiesToUpdate is null | ||
? null | ||
: CompositeTempTableEntityPropertiesProvider.CreateForInsertOrUpdate(PropertiesToInsert, PropertiesToUpdate, KeyProperties), | ||
Advanced = { UsePropertiesToInsertForTempTableCreation = true } | ||
}; | ||
|
||
TempTableOptions.Populate(options); | ||
|
||
return options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.