Problem with Entity Framework extensions .BulkSaveChanges - Saving Guid

.net-core entity-framework-core sqlbulkcopy sqlite

Question

I'm using EF extension to do BulkSaveChanges, and I'm using SQLite, everything works perfectly, fast saving, etc. The problem I'm having is that when saving Guids, it's as if BulkSaveChanges is converting the Guids to TEXT first before saving, but the data type is BLOB, then when retrieved from the database through DbContext, GUIDs are not recognizable, my theory is that because the data is converted and the fact that the data type is BLOB, EF, couldn't convert it back to Guids implicitly.

Moreover, when using the EF's .SaveChanges, everything is preserved, also retrieval is okay, but .SaveChanges is slow.

Any help will be greatly appreciated.

So here's the link to the extension: https://entityframework-extensions.net/bulk-savechanges

Here's some screenshots of data being saved with .SaveChanges vs. .BulkSaveChanges

Saved with .SaveChanges:

enter image description here

Saved with .BulkSaveChanges:

enter image description here

1
0
4/16/2019 3:56:06 AM

Accepted Answer

I was able to make things work and able to live with this situation considering:

  1. That SQLite does not support GUIDs and it gives BLOB data type to data that it can't recognize.
  2. That BulkSaveChanges converts or should I say corrects the data before it's persisted, in this case converts it to TEXT.

The problem however is that the data type remains BLOB, then when retrieved through EFCore, converting back to GUID implicitly, wouldn't work.

Upon analyzing the situation, I realized why can't I just define the data type of the GUIDs to TEXT? since BulkSaveChanges does a conversion to TEXT anyway. And in that way, EFCore would be able to see that the GUIDs are saved as TEXT and not BLOB so it can know how to convert it back to GUID implicitly. Turned out, it's possible!

Here's what I did, I added these lines when we build the model through Fluent API for entities that contain GUIDs:

entityTypeBuilder
    .Property(l => l.GUID)
    .HasConversion<string>();

Building the model looks something like this:

public override void Build(ModelBuilder modelBuilder)
{
    EntityTypeBuilder<Person> entityTypeBuilder =
        modelBuilder.Entity<Person>();

    entityTypeBuilder
        .Property(l => l.GUID)
        .HasConversion<string>();
}

Data type of GUIDs before:

enter image description here

Then after added these lines:

enter image description here

1
4/17/2019 4:20:43 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow