entity-framework-coreazure-cosmosdb

Entity Framework Core Cosmos DB - expression cannot be evaluated


I'm using VS17.10.5 Net 8, Blazor server side.

I have an EF Core context with a simple data class Person (id, name, address, phone) that is set up to point to a Cosmos DB database.

When I call SaveChanges() to save to Cosmos DB, I am getting an error message

The expression cannot be evaluated. A common cause of this error is attempting to pass a lamda into a delegate.

My code is as follows:

Person p = new Person() {id = 1, Name="bob", Address="someAddr", Phone="123", PartitionKey="/person"}
dbContext.Add(p);
dbContext.SaveChanges(); <-- exception

Solution

  • Solution To my question For some reason, EF Core isn't appearing to support database first for Cosmos DB.

    I needed to make a to Ensure created before executing the code.

    dbContext.Databse.EnsureCreated()
    

    This took the entity (builder) definitions in OnModelCreating() in the context and created the Cosmos Containers as required.

    Because I was using the Blazor template, it also required that I implement the schema for the IdentityDbContext which required adding 7 identity entities into 2 additional containers (due to partition key constraints).