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

ExecuteWithGraph: InvalidOperationException with model with subtype within owned type #1337

Open
fretje opened this issue Dec 5, 2023 · 0 comments

Comments

@fretje
Copy link
Contributor

fretje commented Dec 5, 2023

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?

using EFCore.BulkExtensions;
using Microsoft.EntityFrameworkCore;

var connectionString = "Server=localhost;Database=BulkExtensionsTestDb;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=true;";
using var context = new MyDbContext(new DbContextOptionsBuilder().UseSqlServer(connectionString).Options);

context.Database.EnsureCreated();

var bulkConfig = new BulkConfig { IncludeGraph = true, CalculateStats = true };
Customer[] entities = [
    new()
    {
        CustomerId = "test1",
        ContactMethods = new()
        {
            HomePhone = "homephone1",
            EmailAdresses = [
                new Email() { Address = "email1", Type = "emailtype1" },
                new Email() { Address = "email2", Type = "emailtype2" },
            ]
        },
    }
];

await context.BulkInsertOrUpdateAsync(entities, bulkConfig);


public class MyDbContext(DbContextOptions opts) : DbContext(opts)
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Customer>(c => 
            c.OwnsOne(x => x.ContactMethods, 
                cm => cm.OwnsMany(y => y.EmailAdresses)));
    }
}

public class Customer
{
    public string CustomerId { get; set; } = default!;
    public ContactMethods ContactMethods { get; set; } = default!;
}

public class ContactMethods
{
    public string HomePhone { get; set; } = default!;
    public ICollection<Email> EmailAdresses { get; set; } = default!;
}

public class Email
{
    public string Address { get; set; } = default!;
    public string Type { 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant