NorthernLight1 / N.EntityFramework.Extensions

Bulk data support for the EntityFramework 6.5.0+
MIT License
35 stars 20 forks source link

Null reference exception when calling BulkSaveChanges under EF 6.5.1 with Microsoft.Data.SqlClient provider. #138

Closed ScruffyMagicM closed 3 months ago

ScruffyMagicM commented 4 months ago

Describe the bug I'm trying to call BulkSaveChanges, but I'm receiving this error: Object reference not set to an instance of an object.

Additional information: I'm using entity framework 6.5.1 with Microsoft.Data.SqlClient as the provider. The same code worked without issue (6.4.4, System.Data.SqlClient) before I migrated to the newer entity framework version and provider, so I suspect it's relevant. The library describes says it supports 6.4.4+, which I took to mean 6.5.1 was included.

Stack Trace:

at N.EntityFramework.Extensions.DbTransactionContext..ctor(DbContext context, ConnectionBehavior connectionBehavior, TransactionalBehavior transactionalBehavior, Nullable`1 commandTimeout, Boolean openConnection)

Call Stack:

N.EntityFramework.Extensions.dll!N.EntityFramework.Extensions.DbTransactionContext.DbTransactionContext(System.Data.Entity.DbContext context, N.EntityFramework.Extensions.Enums.ConnectionBehavior connectionBehavior, System.Data.Entity.TransactionalBehavior transactionalBehavior, int? commandTimeout, bool openConnection)   Unknown

N.EntityFramework.Extensions.dll!N.EntityFramework.Extensions.DbTransactionContext.DbTransactionContext(System.Data.Entity.DbContext context, N.EntityFramework.Extensions.BulkOptions options) Unknown
    N.EntityFramework.Extensions.dll!N.EntityFramework.Extensions.DbContextExtensions.BulkInsert<object>(System.Data.Entity.DbContext context, System.Collections.Generic.IEnumerable<object> entities, N.EntityFramework.Extensions.BulkInsertOptions<object> options) Unknown
    N.EntityFramework.Extensions.dll!N.EntityFramework.Extensions.DbContextExtensions.BulkInsert<object>(System.Data.Entity.DbContext context, System.Collections.Generic.IEnumerable<object> entities, System.Action<N.EntityFramework.Extensions.BulkInsertOptions<object>> optionsAction)    Unknown
    N.EntityFramework.Extensions.dll!N.EntityFramework.Extensions.DbContextExtensions.BulkSaveChanges(System.Data.Entity.DbContext dbContext, bool acceptAllChangesOnSuccess, bool autoMapOutput)   Unknown
    N.EntityFramework.Extensions.dll!N.EntityFramework.Extensions.DbContextExtensions.BulkSaveChanges(System.Data.Entity.DbContext dbContext)   Unknown

To Reproduce Steps to reproduce the behavior:

Call AddRange method. Call BulkSaveChanges method.

My code is explicitly this:

myContext.MyDbSetEntitites.AddRange(newEntities);
myContext.BulkSaveChanges();

Expected behavior Correct handling of null values.

Desktop (please complete the following information):

OS: Windows Browser: Chrome Version: 126.0.6478.127 Sql Sever: Microsoft SQL Server Express (64-bit) v16.0.1000.6

SpicyCatGames commented 4 months ago

Also happens with BulkInsert() The actual exception is here in N.EntityFramework.Extensions.DbTransactionContext class It seems this.Connection is null image

above that, where it gets the connection in N.EntityFramework.Extensions.DatabaseExtensions, It gets the connection, but when you do as SqlConnection, it becomes null.

PS: noone mentioned it only works with SqlServer. I tried with mysql. What a waste of time. Edit: I see they did. But it should be bold and on top.

penihel commented 4 months ago

i have same problem here.

Same solution @NorthernLight1 ?

penihel commented 4 months ago

@NorthernLight1 , please check my sugestion

142

NorthernLight1 commented 3 months ago

The PR for this bug has been approved and released in v1.9.0.