You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the newest version (6.4.4) I have the problem that the order of the entities to be inserted is changed. Also the entities don't have the database IDs assigned (see Output section "newValuesFromMemory").
Given the following code:
[HttpGet]publicasyncTask<IActionResult>CreateStudents(){varentriesToInsertOrUpdate=newList<Student>{new Student {CourseId=1,EnrollmentDate=new DateTimeOffset(2020,1,1,10,0,0, TimeSpan.FromHours(0)),Name="Fawn Shawna"},// Updatednew Student {CourseId=1,EnrollmentDate=new DateTimeOffset(2022,1,1,23,0,0, TimeSpan.FromHours(0)),Name="Fawn Shawna"},// Created should be 11, but is 14new Student {CourseId=1,EnrollmentDate= DateTimeOffset.UtcNow,Name="Indy Carreen"},// Created should be 12, but is 15new Student {CourseId=null,EnrollmentDate= DateTimeOffset.UtcNow,Name="Nelly Cecil"},// Created should be 13, but is 13new Student {CourseId=null,EnrollmentDate=new DateTimeOffset(2020,1,1,10,0,0, TimeSpan.FromHours(0)),Name="Fawn Shawna"},// Created should be 14, but is 12new Student {CourseId=null,EnrollmentDate=new DateTimeOffset(2020,1,1,10,0,0, TimeSpan.FromHours(0)),Name="Keila Emerson"},// Updatednew Student {CourseId=1,EnrollmentDate= DateTimeOffset.UtcNow,Name="Quentin Creighton"},// Created should be 15, but is 16new Student {CourseId=2,EnrollmentDate=new DateTimeOffset(2020,1,1,8,0,0, TimeSpan.FromHours(0)),Name="Aston Fenton"},// Updatednew Student {CourseId=null,EnrollmentDate= DateTimeOffset.UtcNow,Name="Earlene Aaren"},// Created should be 16, but is 11};varbeforeInsertOrUpdate=await _applicationDbContext.Set<Student>().ToArrayAsync();await _applicationDbContext.BulkInsertOrUpdateAsync(
entities: entriesToInsertOrUpdate,
bulkConfig:new BulkConfig
{SetOutputIdentity=true,PreserveInsertOrder=true,UpdateByProperties=newList<string>{
nameof(Student.CourseId),
nameof(Student.Name),
nameof(Student.EnrollmentDate),},PropertiesToExcludeOnUpdate=newList<string>{
nameof(Student.CreatedAt),},SqlBulkCopyOptions= SqlBulkCopyOptions.CheckConstraints
| SqlBulkCopyOptions.FireTriggers
| SqlBulkCopyOptions.UseInternalTransaction
});return Ok(new{
OldValuesFromDatabase = beforeInsertOrUpdate,
NewValues = entriesToInsertOrUpdate,
ValuesFromDatabase =await _applicationDbContext.Set<Student>().ToArrayAsync()});}
varorderBy=(primaryKeys.Count()==0)?"":$"ORDER BY {GetCommaSeparatedColumns(primaryKeys)}";
sourceTable=$"(SELECT TOP {numberOfEntities} * FROM {sourceTable}{orderBy})";
}
But when UpdateByProperties are configured then orderBy is done with those props instead of ID.
I have tried to change it, and keep IdentityCol, with following line:
varorderByCols= tableInfo.HasIdentity ?newList<string>{ tableInfo.IdentityColumnName }:primaryKeys;varorderBy=(orderByCols.Count()==0)?"":$"ORDER BY {GetCommaSeparatedColumns(orderByCols)}";
But still it does not make proper order.
Seems this is due to how Sql MERGE works, it can only OrberBy cols that are in ON list to be compared for update.
Only alternative would be to first use BulkRead and find which records already exist then split the list into 2 lists entitiesForUpdate and entitiesForInsert without configuring UpdateByProps.
Using the newest version (6.4.4) I have the problem that the order of the entities to be inserted is changed. Also the entities don't have the database IDs assigned (see Output section "newValuesFromMemory").
Given the following code:
Entities:
Context
Output
The text was updated successfully, but these errors were encountered: