Reputation: 43
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
Upvotes: 1
Views: 273
Reputation: 1888
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.
Make sure for the entities that already exist and you aren't going to do any updates on aren't initializing a new DateTime
value on CreationTime
when you call method BulkInsertOrUpdate
.
In the PropertiesToExcludeOnCompare
list, CreationTime
was not added that means if you initialize it i.e
public DateTime CreationTime { get; set; } = DateTime.Now;
(value is now different from what's in the database) and proceed to call BulkInsertOrUpdate
all fields for that record will be updated in the database except ofcourse CreationTime
since it was excluded with PropertiesToExcludeOnUpdate
.
If this is the case solution then would be to add it to the PropertiesToExcludeOnCompare
list in order to avoid doing comparison on CreationTime
between entity and record existing in database.
Upvotes: 0