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
When using BulkInsertOrUpdate with custom UpdateBy columns and SetOutputIdentity && PreserveInsertOrder does not work when some of the UpdateByColums values contains null values. See example:
[Fact]publicvoidBulkInsertOrUpdate_WillNotAssignIds_OnDuplicateValues(){varbulkId=Guid.NewGuid();varexistingItemId="existingDuplicateOf";varinitialItem=newItem2(){StringProperty=existingItemId,Name="initial1",BulkIdentifier=bulkId,};varduplicateInitial=newItem2(){StringProperty=existingItemId,Name="initial2",BulkIdentifier=bulkId,};using(vardb=GetTestContext()){db.Items2.Add(initialItem);db.Items2.Add(duplicateInitial);db.SaveChanges();Assert.NotEqual(0,initialItem.Id);Assert.NotEqual(0,duplicateInitial.Id);}using(vardb=GetTestContext()){varnewItem=newItem2(){StringProperty="A",BulkIdentifier=bulkId,};varupdatedItem=newItem2(){StringProperty=existingItemId,// begins with 'e'BulkIdentifier=bulkId,Name="updated by Bulks",};varnewItem2=newItem2(){StringProperty="ZZZ_will not get assigned",BulkIdentifier=bulkId,};varensureList=new[]{newItem,updatedItem,newItem2};// bug in library - which for some orders will not set some output ids:db.BulkInsertOrUpdate(ensureList,
c =>{c.UpdateByProperties=new[]{nameof(Item2.StringProperty),nameof(Item2.BulkIdentifier)};c.SetOutputIdentity=true;c.PreserveInsertOrder=true;});// Since newItem2 is last in the list (of ordered by StringProp), it will not get assigned the id because of the bug (see above)...Assert.Equal(0,newItem2.Id);// The items were inserted/updatedAssert.Equal(4,GetItems2BulkCount(bulkId));}}
so if there are duplicate values in output values (duplicate in sense of custom UpdateByColumns), then some of the ids of the items might not be set. See example above for specific scenario. Proposed solution:
Just add post check that will verify that all ids were set (are not negative or zero). And if this would be detected, then it which will throw or inform user that the output ids are not reliable.
Fix the code in UpdateEntitiesIdentity, and ensure that id for all items is set, by looping trough the input entities instead of the entities from input table. The only question is then which ids (of possibly mulltiple ids shoudl be set to input entity - first, last ...?)
I will happily create PR with nice tests if you agree (we already do have something simmilar in our fork).
The text was updated successfully, but these errors were encountered:
tracking issue for:
https://github.com/borisdj/EFCore.BulkExtensions/issues/1251
copy of the issue:
The text was updated successfully, but these errors were encountered: