Exception with owned collections with EF Core and CosmosDB provider

.net-core azure-cosmosdb c# entity-framework-core

Question

I have an Entity Framework Core context with CosmosDB provider and only one entity:

public class Entity1
{
    public Guid Id { get; set; }

    public List<Item> Items { get; set; }
}

here are the inner objects of the entity:

public class Item
{
    public Guid Id { get; set; }

    public List<SubItem> SubItems { get; set; }
}

public class SubItem
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

The class Entity1 have an owned property of type collection of Item and this have another owned property of type collection SubItem. This is the configuration file for Entity1:

public class Entity1Configuration : IEntityTypeConfiguration<Entity1>
{
    public void Configure(EntityTypeBuilder<Entity1> builder)
    {
        builder.ToContainer("Entity1");
        builder.HasKey(x => x.Id);

        builder.OwnsMany(x => x.Items, itemsBuilder =>
        {
            itemsBuilder.OwnsMany(t => t.SubItems, subitemsBuilder =>
            {
                subitemsBuilder.HasKey(x => x.Id);
            });
        });
    }
}

Finally, when I'm trying to access the elements of the DbSet<Entity1> Entities1 I get an InvalidOperationException from EF Core:

        CosmosDbContext cosmosDbContext = new CosmosDbContext("https://localhost:8081", "myprivatekey", "Dev");
        cosmosDbContext.Database.EnsureCreated();

        var entities = cosmosDbContext.Entities1.ToList();
        Console.WriteLine(entities.Count);

The Exception is this:

System.InvalidOperationException: 'When called from 'VisitLambda', rewriting a node of type 'System.Linq.Expressions.ParameterExpression' must return a non-null value of the same type. Alternatively, override 'VisitLambda' and change it to not visit children of this type.'

The exception is throwed in the line: var entities = cosmosDbContext.Entities1.ToList();

If I remove the SubItems property from the class Item everything works OK. I also tried change the provider of EF to OnMemory, and everything works perfect. I googled this exception with no luck so far. Can I have inner entities like my case with EF Core and CosmosDB provier?

1
0
11/8/2019 12:46:53 PM

Accepted Answer

Referring to Translation of Lambda in SqlTranslator throws exception, it is a bug which has been fixed in newer EntityFrameworkCore package.

Please upgrade to latest version, for example: 3.1.0-preview2.19525.5. I tested at my side, and the problem was fixed.

1
11/11/2019 9:51:38 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow