EFCore.BulkExtensions icon indicating copy to clipboard operation
EFCore.BulkExtensions copied to clipboard

EF Temporal Table Bulk Read issue

Open onmusic-development opened this issue 3 years ago • 4 comments

I'm having an issue when trying to use the library with one of the new EF Core 6 features. Temporal tables can now be defined explicitly in the EF model, eg.

modelBuilder.Entity<Society>().ToTable("Societies", x => x.IsTemporal());

This correctly creates the tables in my SQL Server db and works with migrations etc. However, the bulk extension code now fails if I try and use BulkRead or BulkInsert with SetOutputIdentity = true.

image

The temporal fields used match the default ones expected by the library in the BulkConfig's TemporalColumns property ("PeriodStart", "PeriodEnd"). I have tried playing around with the BulkConfig quite a bit to find a configuration that works, but I always end up getting one exception or another.

One work around that I have found is simply commenting out the code that sets the temporal table in the model Configuration so that I have the temporal table in my database, but not defined in code. This isn't ideal as I have to remember to re add those definition lines when I want to do a migration and then remove them again to actually run the code.

It would be great if the Bulk Extensions library was compatible with this feature of EF Core out of the box, unless there is some required configuration that I am missing.

onmusic-development avatar Jan 21 '22 16:01 onmusic-development

Can confirm the issue, but don't have the solution at the moment. Until fixed your workaround could be improved by encapsulating modelBuilder config with #if !DEBUG.

#if DEBUG
    modelBuilder.Entity<Society>().ToTable(nameof(Society), a => a.IsTemporal());
#endif

Then to run add-migration in debug mode, while in production it will be skipped for Bulk ops.

borisdj avatar Jan 21 '22 21:01 borisdj

Thanks for taking a look, and for the tip.

onmusic-development avatar Jan 24 '22 08:01 onmusic-development

Any updates? Thanks.

jack0fshad0ws avatar Jun 01 '22 03:06 jack0fshad0ws

Maybe this configuration will help. It worked for us.
There seems to be a couple different default Temporal Column name conventions around - using ValidFrom vs PeriodStart. We were using ValidFrom/ValidTo and adding those to the BulkConfig fixed it.

return new BulkConfig
{
	SqlBulkCopyOptions = SqlBulkCopyOptions.CheckConstraints,

	//Shadow columns used for Temporal table. Has defaults elements: 'PeriodStart' and 'PeriodEnd'. 
        //Changed because our temporal columns have custom names.
	TemporalColumns = new List<string>() { "PeriodStart", "PeriodEnd", "ValidFrom", "ValidTo", },
};

dirq avatar Sep 06 '22 17:09 dirq

Fixed with latest v.

borisdj avatar May 26 '23 12:05 borisdj