I have to fill in CreationTime and LatestUpdate columns in my SQL Server database tables. CreationTime must be DateTime.Now at the time of insert, instead LatestUpdate must be updated to DateTime.Now every time the record is updated. I have to update in bulk a lot of records, so I want to use the the EFCore.BulkExtensions library method BulkInsertOrUpdate.
I managed to fill in CreationTime once forever by putting it by default to the value DateTime.Now and by using bulk configuration PropertiesToExcludeOnUpdate on it.
However, I can't manage to fill in LatestUpdate in the correct way.
I have tried to use bulk configuration PropertiesToExcludeOnCompare after being filled in with DateTime.Now. But this option always updates my LatestUpdate field to Now, whether other columns have been updated or not. I can't understand what this option is for. It seems to work always at the same way.
In the documentation:
By adding a column name to the
PropertiesToExcludeOnCompare, will allow it to be inserted and updated but will not update the row if any of the other columns in that row did not change.
It seems a contradiction in terms. How is a row marked as updated or not? Is there a way to discover, so that I can set latestUpdate column correctly?
In conclusion:
BulkInsertOrUpdate(Entities, new BulkConfig()
{
PropertiesToExcludeOnCompare = new List<string>()
{
nameof(BaseEntity.LatestUpdate)
},
PropertiesToExcludeOnUpdate = new List<string>()
{
nameof(BaseEntity.CreationTime)
}
});
works correctly for CreationTime, but not for LatestUpdate
Make sure for the entities that already exist and you aren't going to do any updates on aren't initializing a new
DateTimevalue onCreationTimewhen you call methodBulkInsertOrUpdate.In the
PropertiesToExcludeOnComparelist,CreationTimewas not added that means if you initialize it i.epublic DateTime CreationTime { get; set; } = DateTime.Now;(value is now different from what's in the database) and proceed to callBulkInsertOrUpdateall fields for that record will be updated in the database except ofcourseCreationTimesince it was excluded withPropertiesToExcludeOnUpdate.If this is the case solution then would be to add it to the
PropertiesToExcludeOnComparelist in order to avoid doing comparison onCreationTimebetween entity and record existing in database.