efcore/EFCore.NamingConventions

OwnsMany + ToJson throw Sequence contains no elements exception

Closed this issue · 4 comments

Relates to #173, but with different configuration.

public class ResponseDocument
{
    //...
    public ICollection<FileMetadata>? Files { get; set; }
}
public record FileMetadata
{
    public Guid FileId { get; set; }
    public string Name { get; set; } = "";
    public int Size { get; set; }
}

And configuration

internal class ResponseDocumentConfiguration : IEntityTypeConfiguration<ResponseDocument>
{
    public void Configure(EntityTypeBuilder<ResponseDocument> builder)
    {
        builder.OwnsMany(r => r.Files, nav => nav.ToJson());
    }
}

Thrown exception

System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.ValidateJsonEntities(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()

Tested with 7.0.0 and 7.0.2: same error.

Did you try 8.0.0-rc.2?

Hum not yet.
We just migrated to .net8 (not yet to EF8).
I'll test it shortly.

Seems like 8.0.0-rc.2 has issues too: #234

roji commented

I've tried to reproduce the exception with the latest bits (about to be released as 8.0.0) and could not see an exception. It's very well possible that this has been fixed (I've done some work in the area), but the sample above is incomplete - it's always best to submit a minimal, runnable code sample.

I'll go ahead and close this now - please try 8.0.0 when it's released. Below is my attempted repro - you can tweak it to show it failing if the bug is still there.

Attempted repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

public class BlogContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql("Host=localhost;Username=test;Password=test")
            .UseSnakeCaseNamingConvention()
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ResponseDocument>()
            .OwnsMany(r => r.Files, nav => nav.ToJson());
    }
}

public class ResponseDocument
{
    public int Id { get; set; }
    public ICollection<FileMetadata>? Files { get; set; }
}

public record FileMetadata
{
    public Guid FileId { get; set; }
    public string Name { get; set; } = "";
    public int Size { get; set; }
}