You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've tried to make a minimal reproducible sample. The crux of the issue (I think) is that my model has a subtype (an ICollection<Email>) within an owned type (ContactMethods), and somehow the foreign keys (ContactMethodsCustomerId) of the Email entities don't get filled before sending them to sqlbulkcopy?
usingEFCore.BulkExtensions;usingMicrosoft.EntityFrameworkCore;varconnectionString="Server=localhost;Database=BulkExtensionsTestDb;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=true;";usingvarcontext=newMyDbContext(newDbContextOptionsBuilder().UseSqlServer(connectionString).Options);context.Database.EnsureCreated();varbulkConfig=newBulkConfig{IncludeGraph=true,CalculateStats=true};Customer[]entities=[new(){CustomerId="test1",ContactMethods=new(){HomePhone="homephone1",EmailAdresses=[newEmail(){Address="email1",Type="emailtype1"},newEmail(){Address="email2",Type="emailtype2"},]},}];awaitcontext.BulkInsertOrUpdateAsync(entities,bulkConfig);publicclassMyDbContext(DbContextOptionsopts):DbContext(opts){protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<Customer>(c =>c.OwnsOne(x =>x.ContactMethods,
cm =>cm.OwnsMany(y =>y.EmailAdresses)));}}publicclassCustomer{publicstringCustomerId{get;set;}=default!;publicContactMethodsContactMethods{get;set;}=default!;}publicclassContactMethods{publicstringHomePhone{get;set;}=default!;publicICollection<Email>EmailAdresses{get;set;}=default!;}publicclassEmail{publicstringAddress{get;set;}=default!;publicstringType{get;set;}=default!;}
This is the Exception which is thrown on await context.BulkInsertOrUpdateAsync(entities, bulkConfig);
System.InvalidOperationException
HResult=0x80131509
Message=Column 'ContactMethodsCustomerId' does not allow DBNull.Value.
Source=Microsoft.Data.SqlClient
StackTrace:
at Microsoft.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed)
at Microsoft.Data.SqlClient.SqlBulkCopy.ReadWriteColumnValueAsync(Int32 col)
at Microsoft.Data.SqlClient.SqlBulkCopy.CopyColumnsAsync(Int32 col, TaskCompletionSource`1 source)
at Microsoft.Data.SqlClient.SqlBulkCopy.CopyRowsAsync(Int32 rowsSoFar, Int32 totalRows, CancellationToken cts, TaskCompletionSource`1 source)
at Microsoft.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at Microsoft.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at Microsoft.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source)
--- End of stack trace from previous location ---
at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.<InsertAsync>d__6`1.MoveNext()
at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.<InsertAsync>d__6`1.MoveNext()
at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.<InsertAsync>d__6`1.MoveNext()
at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.<InsertAsync>d__5`1.MoveNext()
at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.<MergeAsync>d__9`1.MoveNext()
at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.<MergeAsync>d__9`1.MoveNext()
at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.<MergeAsync>d__8`1.MoveNext()
at EFCore.BulkExtensions.SqlBulkOperation.<MergeAsync>d__5`1.MoveNext()
at EFCore.BulkExtensions.DbContextBulkTransactionGraphUtil.<ExecuteWithGraphAsync>d__2.MoveNext()
at EFCore.BulkExtensions.DbContextBulkTransactionGraphUtil.<ExecuteWithGraphAsync>d__2.MoveNext()
at EFCore.BulkExtensions.DbContextBulkTransactionGraphUtil.<ExecuteWithGraphAsync>d__1.MoveNext()
at EFCore.BulkExtensions.DbContextBulkTransaction.<ExecuteAsync>d__1`1.MoveNext()
at Program.<<Main>$>d__0.MoveNext() in C:\Projects\test\ConsoleAppBulkExtensions\ConsoleAppBulkExtensions\Program.cs:line 25
The text was updated successfully, but these errors were encountered:
I've tried to make a minimal reproducible sample. The crux of the issue (I think) is that my model has a subtype (an
ICollection<Email>
) within an owned type (ContactMethods
), and somehow the foreign keys (ContactMethodsCustomerId
) of theEmail
entities don't get filled before sending them to sqlbulkcopy?This is the Exception which is thrown on
await context.BulkInsertOrUpdateAsync(entities, bulkConfig);
The text was updated successfully, but these errors were encountered: