NorthernLight1 / N.EntityFrameworkCore.Extensions

Bulk data support for the EntityFrameworkCore 8.0.0+
MIT License
31 stars 13 forks source link

Exception in BulkMerge when using composite primary key #85

Open osmozis opened 3 weeks ago

osmozis commented 3 weeks ago

dbContext.BulkMerge() method generates exception "Unable to drop table '#tmp_be_xx...' because it doesn't exist or you don't have permissions"

This kind of exception only thrown in case of using composite primary key as in modelBuilder.Entity<Example>() .Property(x => x.Key1) .Property(x => x.Key2) .HasKey(x => new { x.Key1, x.Key2 })

The problem happens due to wrong SQL code generated for the MERGE query MERGE [Example] AS t USING [#tmp_be_xx_Example] AS s ON t.Key1=s.Key1ANDt.Key2=s.Key2...... <- there are no spaces around 'AND' keyword

There are actually 2 exceptions: -first exception is about wrong SQL statement, but it is being swallowed by the library -the second one is about temp table dropping error that happens during BulkOperation.Dispose

Jackobii commented 1 week ago

I'm encountering the same issue. When using composite keys the GetJoinConditionSql in CommonUtil.cs will add "AND" without any spaces when building the query.

Compare this to the same method in the N.EntityFramework.Extensions where it will instead add " AND " with spaces. N.EntityFrameworkCore.Extensions: {2169A04B-B698-40F4-A71D-57BE77596A2A} N.EntityFramework.Extensions: {3324EAA5-7930-4993-B5C9-80829164B751}

Hopefully this should be all thats needed to fix the issue 😄