efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Cosmos Hangs on OnModelCreating While Configuring Entity

Open EnisMulic opened this issue 1 year ago • 4 comments

File a bug

I have configured several entities to work with a single Cosmos Container. Now I'm trying to configure another entity to work with a second container but ef core hand while trying to map to container. I've tested several cases and this seems to only happen while my entity owns other entities.

Include your code

public abstract class AuditableEntity 
{
    public Guid Id {get; set; }
    public DateTime Created { get; set; }
    public string? CreatedBy { get; set; }
    public DateTime? LastModified { get; set; }
    public string? LastModifiedBy { get; set; }
}

public class Container1 : AuditableEntity
{
   public string Container1Id {get; set; }
   public string Name {get; set; }
}

public class Container2 : AuditableEntity
{
   public string Container2Id {get; set; }
   public string Name {get; set; }
}

public class CosmosContext : DbContext
{
    public CosmosContext(DbContextOptions<CosmosContext> options) : base(options)
    {
    }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Container1>().ToContainer("Container1")
           .HasPartitionKey(i => i.Container1Id);

        modelBuilder.Entity<Container1>().Property(i => i.Id)
            .HasConversion<string>();

       // Hangs here
        modelBuilder.Entity<Container2>().ToContainer("Container2")
           .HasPartitionKey(i => i.Container2Id);

        modelBuilder.Entity<Container2>().Property(i => i.Id)
            .HasConversion<string>();

        base.OnModelCreating(modelBuilder);
    }
}

I've also tried adding owned entities to "Container2" but that causes the same issue

public class Foo
{
   public string Bar {get; set;}
}
public class Container2
{
   public string Container2Id {get; set; }
   public string Name {get; set; }
   public Foo Foo {get; set;}
}

Include provider and version information

EF Core version: Database provider: (e.g. Microsoft.EntityFrameworkCore.Cosmos) Target framework: (e.g. .NET 7.0) Operating system: IDE: (e.g. Visual Studio for Mac Version 17.6.8 (build 400))

Cosmos Configuration

Capacity mode: Provisioned throughput Total throughput limit: 1600 RU/s Both containers set to 400 RU/s (Default settings), both created using Microsoft.Azure.Cosmos.CosmosClient

EnisMulic avatar Feb 11 '24 13:02 EnisMulic

I've continued debugging and I seem to be running into issues only if the PartitionKey if "Container2Id" but if i set it to "ContainerId1" or "Id" it seems to work

Edit: Second update, after more attempts I've found out that the issue is caused while the entity "Container2" has an Id property, and removing it also resolves the issue. (Note: "Container1" also has this Id property and doesn't cause any issues)

EnisMulic avatar Feb 11 '24 15:02 EnisMulic

I am not able to reproduce this--see my code below. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

Code
using var context = new CosmosContext();
Console.WriteLine(context.Model.ToDebugString());

public abstract class AuditableEntity 
{
    public Guid Id {get; set; }
    public DateTime Created { get; set; }
    public string? CreatedBy { get; set; }
    public DateTime? LastModified { get; set; }
    public string? LastModifiedBy { get; set; }
}

public class Container1 : AuditableEntity
{
    public string Container1Id {get; set; }
    public string Name {get; set; }
}

public class Container2 : AuditableEntity
{
    public string Container2Id {get; set; }
    public string Name {get; set; }
}

public class CosmosContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseCosmos(
                "https://localhost:8081",
                "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
                "Daily")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Container1>().ToContainer("Container1")
            .HasPartitionKey(i => i.Container1Id);

        modelBuilder.Entity<Container1>().Property(i => i.Id)
            .HasConversion<string>();

        // Hangs here
        modelBuilder.Entity<Container2>().ToContainer("Container2")
            .HasPartitionKey(i => i.Container2Id);

        modelBuilder.Entity<Container2>().Property(i => i.Id)
            .HasConversion<string>();

        base.OnModelCreating(modelBuilder);
    }
}

ajcvickers avatar Feb 12 '24 10:02 ajcvickers

https://github.com/EnisMulic/EfCosmosRepro/

I've ran it against a cosmos db on azure, as I am using a Mac M2 and can't run cosmos in docker

EnisMulic avatar Feb 12 '24 12:02 EnisMulic

@EnisMulic: Thanks for the repro. Team: Still repros on latest daily; not a regression.

ajcvickers avatar Feb 13 '24 09:02 ajcvickers