0

I'm having a problem bulk inserting data using EF Extensions library by ZZZ Projects that the records should be unique for specific tables (lookup tables) and I'm importing files to the database so the records can be repeated, I can't find a way to make the library ignore existing data

I'm not sure which options to use when bulk inserting records that can exist multiple times.

2 Answers 2

2

You need the InsertIfNotExists option like below. The ColumnPrimaryKeyExpression is miss named. It can be any field in your data that you want unique.

await dbContext
    .BulkInsertAsync(sourceFiles, options =>
    {
        options.BatchSize = BatchSize;
        options.AutoMapOutputDirection = false;
        options.InsertIfNotExists = true;
        options.InsertKeepIdentity = false;
        options.ColumnPrimaryKeyExpression = sf => new { sf.<YourUniqueField>};
    })
    .ConfigureAwait(false);
Sign up to request clarification or add additional context in comments.

2 Comments

the insert if not exists is set to true yet I keep getting the same error "Violation of PRIMARY KEY constraint ''. Cannot insert duplicate key in object ''. The duplicate key value is.
does this check the items that haven't saved yet?
0

You can use " options.InsertIfNotExists = true; " As below:

context.BulkInsert(customers, options => { 
    options.InsertIfNotExists = true;
    options.PrimaryKeyExpression = customer => customer.Code;
  });

The options parameter let you use a lambda expression to customize the way entities are inserted

I suggest visiting entityframeworkExtensions

1 Comment

I've added that as well and the library insists on adding the records, what I'm trying to avoid is adding records that are already in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.