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

BulkInsert with TPT on PostgreSQL #785

Closed
Abdragiz opened this issue Mar 30, 2022 · 1 comment
Closed

BulkInsert with TPT on PostgreSQL #785

Abdragiz opened this issue Mar 30, 2022 · 1 comment
Labels

Comments

@Abdragiz
Copy link

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");
        }
    }
}```
@Abdragiz
Copy link
Author

Abdragiz commented Apr 1, 2022

Adding SqlBulkCopyOptions = SqlBulkCopyOptions.KeepIdentity to bulkConfig solved problem.

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