Skip to content

EF Core applies string length conventions to ComplexCollection string properties #37256

@firasdarwish

Description

@firasdarwish

Bug description

I use a custom EF Core convention that applies a maximum length of 255 to all string properties. After upgrading to .NET 10 and starting to use ComplexCollection to store some domain classes as JSON, EF Core still applies HasMaxLength(255) to string properties inside the complex collection

However, according to issue #37140 , JSON-mapped complex types should not have length constraints applied; this matches my expectations

But EF Core still generates the following in the model snapshot:

b.ComplexCollection(typeof(List<Dictionary<string, object>>), "Movies", "Terra.Testing.Domain.User.Movies#Movies", b1 =>
{
    b1.IsRequired();

    b1.Property<string>("Name")
        .IsRequired()
        .HasMaxLength(255); // this `HasMaxLength` causes compile-time error

    b1
        .ToJson("Movies")
        .HasColumnType("json");
});

This leads to a compile-time error:

Error CS1061: 'ComplexCollectionTypePropertyBuilder<string>' does not contain a definition for 'HasMaxLength'

Expected Behavior

String properties inside ComplexCollection entries (serialized to JSON) should not have string length conventions applied.

Actual Behavior

The global string length convention is still applied to nested string properties within ComplexCollection, causing:

  • A snapshot containing invalid builder calls (HasMaxLength)
  • A migration Designer file that does not compile

Question

Is there a supported way to exempt ComplexCollection properties from global string conventions?


Reproduction

  1. Add a convention that applies .HaveMaxLength(255) to all strings.
  2. Define a POCO with a collection property containing string fields.
  3. Map the collection with ComplexCollection() and .ToJson().
  4. Add a migration → snapshot contains invalid HasMaxLength calls.

Your code

// Convention

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
        configurationBuilder.Properties<string>().HaveMaxLength(255);
}



// POCO & Complex type

public sealed class User 
{
   public int Id { get; set; }
   public List<Movies> Movies { get; set; } = [];
}




public sealed record Movies
{
    public required string Name { get; set; }
}



// Complex collection

builder.ComplexCollection(p => p.Movies, b => b.ToJson().HasColumnType("json"));

Stack traces


Verbose output


EF Core version

10.0.0

Database provider

Npgsql.EntityFrameworkCore.PostgreSQL

Target framework

.NET 10.0

Operating system

Windows 11

IDE

JetBrains Rider

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions