dotnet/efcore · error · InvalidOperationException

The expression '{sqlExpression}' in the SQL tree does not ha

Error message

The expression '{sqlExpression}' in the SQL tree does not have a type mapping assigned.

What it means

Thrown by an internal SqlTypeMappingVerifyingExpressionVisitor that walks the translated Cosmos SQL tree and asserts every SqlExpression has a CoreTypeMapping assigned. A null TypeMapping means the provider produced a SQL fragment for a CLR type it cannot serialize to/from Cosmos JSON. This signals either an unsupported CLR type in the query or a bug in the Cosmos query translator.

Source

Thrown at src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.cs:1199

    //     public IEntityType EntityType { get; }
    //
    //     public override Type Type { get; }
    //
    //     public override ExpressionType NodeType
    //         => ExpressionType.Extension;
    //
    //     public Expression Convert(Type type)
    //         => type == typeof(object) // Ignore object conversion
    //             || type.IsAssignableFrom(Type) // Ignore conversion to base/interface
    //                 ? this
    //                 : new EntityReferenceExpression(ParameterEntity, type);
    // }

    private sealed class SqlTypeMappingVerifyingExpressionVisitor : ExpressionVisitor
    {
        protected override Expression VisitExtension(Expression extensionExpression)
            => extensionExpression is SqlExpression { TypeMapping: null } sqlExpression
                ? throw new InvalidOperationException(CosmosStrings.NullTypeMappingInSqlTree(sqlExpression.Print()))
                : base.VisitExtension(extensionExpression);
    }
}

View on GitHub (pinned to dbf9771522)

Solutions

  1. Read the expression printed in the message to find which property/expression lacks a mapping.
  2. Ensure the property's CLR type is one Cosmos supports (primitives, string, Guid, DateTime, TimeSpan, enums, etc.) or configure a value converter to a supported storage type via HasConversion<string>() / HasConversion<T>.
  3. If the type should be supported, check for a missing Cosmos type mapping source registration and report a provider bug if mapping resolution legitimately returns null.
  4. As a workaround, project/compute the value client-side so it is excluded from the server-translated tree.

Example fix

// before: ulong property with no Cosmos mapping
modelBuilder.Entity<Order>().Property(o => o.Flags);
var q = ctx.Orders.Where(o => o.Flags > 0);

// after: convert to a Cosmos-mapped primitive
modelBuilder.Entity<Order>()
    .Property(o => o.Flags)
    .HasConversion<long>();
Defensive patterns

Strategy: validation

Validate before calling

// Verify each property on the queried entity has a Cosmos type mapping before running the query.
foreach (var prop in typeof(Order).GetProperties())
{
    var mapping = cosmosModel.FindEntityType(typeof(Order))
        ?.FindProperty(prop.Name)?.GetTypeMapping();
    if (mapping is null)
        Console.WriteLine($"No Cosmos mapping for {prop.Name} ({prop.PropertyType}); add HasConversion.");
}

Type guard

// Narrow to properties known to have a mapping before projecting them.
static bool HasCosmosMapping(IProperty p) => p.GetTypeMapping() is not null;

Try / catch

try { var results = query.ToList(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("does not have a type mapping"))
{
    // log the offending expression, fall back to client evaluation
}

Prevention

When it happens

Trigger: A LINQ query references a property whose CLR type has no Cosmos type mapping (custom struct, unmapped 3rd-party type, or a value converter yielding an unmappable storage type). The verifier runs at the end of CosmosSqlTranslatingExpressionVisitor translation and fails on the offending expression (printed in the message).

Common situations: Using owned/complex types with unusual CLR property types; custom value converters that convert to a type Cosmos doesn't recognize; querying a property added without configuration after an EF Core upgrade that changed type-mapping resolution.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/88451cca301725b7. Report an issue: GitHub.