efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Cosmos db complex properties can be configured but are not working.

Open JoasE opened this issue 2 weeks ago • 8 comments

Bug description

Complex properties in cosmos db can be configured, but are not stored in the db, nor are they retrieved or can be queried. Originally: #31253 was named: Add Cosmos support for complex types, but was renamed to: Add Cosmos support for complex type collections Complex collections throw when configured in Cosmos db, but complex properties do not. I'd say this is a bug as they can be configured, but don't work. However a solution could be to just implement complex properties.

Your code

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

using var context = new Context(
    new DbContextOptionsBuilder()
    .UseCosmos("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "Testing")
    .Options);

await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

context.Add(new Entity
{
    ComplexType = new ComplexType
    {
        Name = "Test"
    },
});
await context.SaveChangesAsync();


context.ChangeTracker.Clear();
var entity = await context.Set<Entity>().ToListAsync();

Console.WriteLine(entity[0].ComplexType.Name == "Test"); // False

public class Context : DbContext
{
    public Context(DbContextOptions options) : base(options)
    {
    }

    protected Context()
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Entity>(entity =>
        {
            entity.ComplexProperty(x => x.ComplexType);
        });
    }
}

public class Entity
{
    public Guid Id { get; set; } = Guid.NewGuid();

    public ComplexType ComplexType { get; set; }
}


public class ComplexType
{
    public int Id { get; set; }

    public string Name { get; set; }
}

EF Core version

10.0.0 and main branch (11.0.0)

Database provider

Microsoft.EntityFrameworkCore.Cosmos

Target framework

.NET 10

Operating system

Windows 11

IDE

VS 2026

JoasE avatar Dec 04 '25 11:12 JoasE