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.

Mohamed Said
  • 524
  • 7
  • 27

2 Answers2

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);
Edney Holder
  • 1,140
  • 8
  • 22
  • 1
    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. – Mohamed Said Jun 06 '20 at 06:21
  • does this check the items that haven't saved yet? – h3n Jan 30 '23 at 09:24
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

Hesam Akbari
  • 1,071
  • 1
  • 5
  • 14
  • 1
    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. – Mohamed Said Jun 06 '20 at 06:22