EFCore BulkExtensions BulkInsertAndUpdate Not adding Foreign Key

Viewed 25

I'm using the BulkExtensions library for EF Core. I have an object that contains a reference to another item example below

Child

public class Location
{
  public string Id;
  public string Description;
  public string Item
}

Parent

public class Parent
{
   public Guid Id;
   public Location Location;
   public string Name;
   public string Service;
   .....
}

I receive a list of Parent objects to add to my Postgres DB. I have the following code to add the items

List<Parent> parentObjects;  // this is being passed in
var locations = _dbcontext.Locations().ToList();
foreach (var item in parentObjects)
{
  item.Location = locations.First(x => x.Id == item.Location.Id);
  // Additional logic

}

var bulkConfig = new BulkConfig()
{
    //IncludeGraph = true,
    CalculateStats = true,               
    PropertiesToIncludeOnUpdate = new List<string> { string.Empty }
};
               
    await _dbContext.BulkInsertOrUpdateAsync(parentObjects, cancellationToken: cancellationToken, 
        bulkConfig: bulkConfig).ConfigureAwait(false);
    await _dbContext.BulkSaveChangesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);

This adds the item to the table but the column for the foreign key locationId is always null. Is there configuration option that I am missing when calling the BulkInsertOrUpdate method?

If I add the IncludeGraph option for BuilkConfig, I get an exception that the Key is not found in the dictionary for the FK column.

0 Answers
Related