mtanneryd / ef-bulk-operations

Bulk operations for Entity Framework 6
Apache License 2.0
80 stars 30 forks source link

BulkInsertAll failling for table with no non primary key columns #24

Closed JKorf closed 4 years ago

JKorf commented 5 years ago

Hi, I'm trying to use your library, but BulkInsertAll gives me an SqlException: Invalid syntax near ','. I tracked the problem down to this line: https://github.com/mtanneryd/ef6-bulk-operations/blob/7ca96c682037bad9c40de83c0041abe3f8ee5005/Tanneryd.BulkOperations.EF6/Tanneryd.BulkOperations.EF6/DbContextExtensions.cs#L1769 The issue is that 1 of my tables only has a primay key. It means that columnNames is an empty string, and it generates an sql statement like this:

MERGE [dbo].[TraitTargets]
                        USING 
                            (SELECT , rowno
                             FROM   tempdb..#9ada186c0c524a4aa88daa44962c87a4) t (, rowno)
                        ON 1 = 0
                        WHEN NOT MATCHED THEN
                        INSERT ()
                        VALUES ()
                        OUTPUT t.rowno,
                               inserted.[TraitTargetId]; 

which is indeed invalid sql. You should probably check if nonPrimaryKeyColumnMappings has any values. Any chance for a quick fix?

mtanneryd commented 5 years ago

I'll take a look tonight. Have some other additions on the way planned for tonight anyway. I'll try to upload a new beta asap.

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd

https://github.com/mtanneryd/ef6-bulk-operations https://github.com/mtanneryd/ef6-bulk-operations

Den ons 6 nov. 2019 kl 16:33 skrev Jan Korf notifications@github.com:

Hi, I'm trying to use your library, but BulkInsertAll gives me an SqlException: Invalid syntax near ','. I tracked the problem down to this line: https://github.com/mtanneryd/ef6-bulk-operations/blob/7ca96c682037bad9c40de83c0041abe3f8ee5005/Tanneryd.BulkOperations.EF6/Tanneryd.BulkOperations.EF6/DbContextExtensions.cs#L1769 The issue is that 1 of my tables only has a primay key. It means that columnNames is an empty string, and it generates an sql statement like this:

MERGE [dbo].[TraitTargets] USING (SELECT , rowno FROM tempdb..#9ada186c0c524a4aa88daa44962c87a4) t (, rowno) ON 1 = 0 WHEN NOT MATCHED THEN INSERT () VALUES () OUTPUT t.rowno, inserted.[TraitTargetId];

which is indeed invalid sql. You should probably check if nonPrimaryKeyColumnMappings has any values. Any chance for a quick fix?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mtanneryd/ef6-bulk-operations/issues/24?email_source=notifications&email_token=AA2BSRPWPWGSBWNOQW5FRJLQSLPVHA5CNFSM4JJWVLF2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HXINYOA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA2BSRNIWJMZHAGRMQTRV53QSLPVHANCNFSM4JJWVLFQ .

mtanneryd commented 5 years ago

I just uploaded 1.3.0-beta1. I have not tested it properly but it just might do the trick. The API has changed a bit so you need to change your calls to BulkInsert and use the new format of the BulkInsertRequest. No big changes but still breaking changes.

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd

https://github.com/mtanneryd/ef6-bulk-operations https://github.com/mtanneryd/ef6-bulk-operations

Den ons 6 nov. 2019 kl 16:33 skrev Jan Korf notifications@github.com:

Hi, I'm trying to use your library, but BulkInsertAll gives me an SqlException: Invalid syntax near ','. I tracked the problem down to this line: https://github.com/mtanneryd/ef6-bulk-operations/blob/7ca96c682037bad9c40de83c0041abe3f8ee5005/Tanneryd.BulkOperations.EF6/Tanneryd.BulkOperations.EF6/DbContextExtensions.cs#L1769 The issue is that 1 of my tables only has a primay key. It means that columnNames is an empty string, and it generates an sql statement like this:

MERGE [dbo].[TraitTargets] USING (SELECT , rowno FROM tempdb..#9ada186c0c524a4aa88daa44962c87a4) t (, rowno) ON 1 = 0 WHEN NOT MATCHED THEN INSERT () VALUES () OUTPUT t.rowno, inserted.[TraitTargetId];

which is indeed invalid sql. You should probably check if nonPrimaryKeyColumnMappings has any values. Any chance for a quick fix?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mtanneryd/ef6-bulk-operations/issues/24?email_source=notifications&email_token=AA2BSRPWPWGSBWNOQW5FRJLQSLPVHA5CNFSM4JJWVLF2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HXINYOA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA2BSRNIWJMZHAGRMQTRV53QSLPVHANCNFSM4JJWVLFQ .

mtanneryd commented 4 years ago

I believe this has been fixed. Please re-open if this is not so.