Skip to content

BulkInsert with TPT on PostgreSQL #785

Closed
@Abdragiz

Description

Tried #493 (comment) for bulk insert in PostgreSQL, but it throws exeption during inserting in inherited table (something about Id NOT NULL). Am I missing something? Repro below.

#pragma warning disable CA1812
using ConsoleApp1;

using EFCore.BulkExtensions;

using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;

[assembly: CLSCompliant(false)]
DbContextOptionsBuilder<LogDbContext> dbContextOptionsBuilder = new();
dbContextOptionsBuilder.UseNpgsql("");
using LogDbContext context = new(dbContextOptionsBuilder.Options);
await context.Database.EnsureDeletedAsync().ConfigureAwait(false);
await context.Database.EnsureCreatedAsync().ConfigureAwait(false);
int nextLogId = 0;
List<LogPersonReport> entities = new();
for (int i = 1; i <= 1000; i++)
{
    nextLogId++;
    LogPersonReport entity = new()
    {
        LogId = nextLogId,
        PersonId = i % 22,
        RegBy = 15,
        CreatedDate = DateTime.UtcNow,
        ReportId = i % 22 * 10,
        LogPersonReportTypeId = 4
    };
    entities.Add(entity);
}

BulkConfig bulkConfigBase = new()
{
    SqlBulkCopyOptions = SqlBulkCopyOptions.KeepIdentity,
    PropertiesToInclude = new List<string>
    {
        nameof(LogPersonReport.LogId),
        nameof(LogPersonReport.PersonId),
        nameof(LogPersonReport.RegBy),
        nameof(LogPersonReport.CreatedDate)
    }
};
BulkConfig bulkConfig = new()
{
    PropertiesToInclude = new List<string>
    {
        nameof(LogPersonReport.LogId),
        nameof(LogPersonReport.ReportId),
        nameof(LogPersonReport.LogPersonReportTypeId)
    }
};

await context.BulkInsertAsync(entities, bulkConfigBase, type: typeof(Log)).ConfigureAwait(false); // works
await context.BulkInsertAsync(entities, bulkConfig).ConfigureAwait(false); // fails with LogId NOT NULL constraint

namespace ConsoleApp1
{
    public abstract class Log
    {
        public int LogId { get; set; }
        public int PersonId { get; set; }
        public int RegBy { get; set; }
        public DateTime CreatedDate { get; set; }
    }

    public class LogPersonReport : Log
    {
        public int ReportId { get; set; }
        public int LogPersonReportTypeId { get; set; }
    }

    public class LogDbContext : DbContext
    {
        public LogDbContext(DbContextOptions<LogDbContext> options) : base(options)
        {
        }

        public DbSet<Log> Logs => Set<Log>();
        public DbSet<LogPersonReport> LogPersonReports => Set<LogPersonReport>();

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            if (modelBuilder == null)
            {
                throw new ArgumentNullException(nameof(modelBuilder));
            }

            modelBuilder.Entity<Log>().ToTable("Logs");
            modelBuilder.Entity<LogPersonReport>().ToTable("LogPersonReports");
        }
    }
}```

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions