Skip to content

BulkSaveChanges with entity containing ComplexProperty mapped to JSON column fails #659

Description

@isak1080

Even if I'm not even saving an entity of that type, the fact that it's part of the model, reachable by a navigation property, makes BulkSaveChangesAsync fail with the following error:

System.Exception: Oops! You are currently using a complex type with a collection (mapped to JSON). This scenario is not supported yet.
   at Z.EntityFramework.Extensions.EntityTypeZInfo.(IComplexProperty )
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at Z.EntityFramework.Extensions.EntityTypeZInfo..ctor(IEntityType entityType)
   at PublicExtensions..(IEntityType )
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at PublicExtensions.ToZInfo(IEntityType entityType)
   ...
   at DbContextExtensions.BulkSaveChanges(DbContext this, Action`1 options)

Reproduce

Sample program that reproduces it:

using System.ComponentModel.DataAnnotations.Schema;

using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;

var connection = new SqliteConnection("Filename=:memory:");
connection.Open();

var options = new DbContextOptionsBuilder<AppDbContext>()
    .UseSqlite(connection)
    .Options;

await using var db = new AppDbContext(options);
await db.Database.EnsureCreatedAsync();

// A single entity, no relationships, no collection navigations anywhere in the model.
var parent = new Parent { Id = Guid.NewGuid() };
db.Parents.Add(parent);

// Throws: "Oops! You are currently using a complex type with a collection (mapped to JSON).
// This scenario is not supported yet."
await db.BulkSaveChangesAsync();

Console.WriteLine("No exception");

[ComplexType]
public class ParentSettings
{
    public bool EnableFeature { get; set; }
}

public class Parent
{
    public Guid Id { get; set; }
    public ParentSettings Settings { get; set; } = new();
}

public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
    public DbSet<Parent> Parents => Set<Parent>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Parent>()
            // Removing .ToJson() here (falling back to EF's default table-split complex type
            // mapping) makes the exception go away entirely — that's the exact trigger.
            .ComplexProperty<ParentSettings>(p => p.Settings, cb => cb.ToJson());
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions