Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BulkInsertOrUpdate not setting Ids automatically #556

Closed
jrobsondev opened this issue May 21, 2021 · 4 comments
Closed

BulkInsertOrUpdate not setting Ids automatically #556

jrobsondev opened this issue May 21, 2021 · 4 comments
Labels

Comments

@jrobsondev
Copy link

I am having an issue using the BulkInsertOrUpdate functionality whilst trying to insert a list of records into a sqlite database.

public async Task BulkInsertOrUpdate(List<TEntity> entities)
{
    var bulkConfig = new BulkConfig
    {
        SetOutputIdentity = true,
        PreserveInsertOrder = false
    };
    await _context.BulkInsertOrUpdateAsync(entities, bulkConfig);
}

The list contains a basic list of entities with the following properties:

Id - int
Version - byte[]
FirstName - string
LastName - string

When the entities are created, they do not have an Id set i.e. Id=0. When I use BulkInsertOtUpdate, only 1 record is added to the database. This record is generally the last item in the list and it has an Id of 0.

When I use the BulkInsert method, the records are inserted correctly and their Ids are automatically set.
I can also insert the entities if I manually set the Id myself, however, I would like to avoid this as it will crreate a large overhead for me if I had to do this for the more complex entities.

@borisdj
Copy link
Owner

borisdj commented May 21, 2021

You should leave PreserveInsertOrder to true

@jrobsondev
Copy link
Author

You should leave PreserveInsertOrder to true

I have set this to true as you suggested and tried it with a list of 709 items. The problem still persists and only one item was added to the database.

image

(ignore the column names, this is a different entity to the one in the first post).

@borisdj
Copy link
Owner

borisdj commented May 22, 2021

It's actually up to Sqlite itself, which does not have full MERGE capabilities like SqlServer.
It only supports simple Upsert with ON CONFLICT command.
So you can either:
a) split list into 2 lists, and call separately BulkInsert and BulkUpdate or
b) if want to keep all in single method then you would need to read last Id and for news ones set Id from that number incrementally.
I would use chose option a), it's simpler.

Will add a comment about this in the ReadMe.

@jrobsondev
Copy link
Author

Thanks for the clarification. This is what I have ended up doing for the meantime, so I will just stick with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants