Home > Mobile >  MartenDB: Unsupported operator 'Coalesce' in a field member expression (Parameter 'no
MartenDB: Unsupported operator 'Coalesce' in a field member expression (Parameter 'no

Time:01-31

.NET7

After upgrading Marten from 4 to 5.11, this code leads to an exception:

storeOptions.Schema.For<UserSession>()
    .Identity(x => x.Key)
    .Duplicate(x => x.SessionId ?? "");

The exception starts out:

ERROR: System.ArgumentOutOfRangeException: Unsupported operator 'Coalesce' in a field member expression (Parameter 'node')
   at Marten.Linq.Parsing.FindMembers.VisitBinary(BinaryExpression node)
   at System.Linq.Expressions.ExpressionVisitor.
VisitLambda[T](Expression`1 node)
   at Marten.Schema.DocumentMapping`1.Duplicate(Expression`1 expression, String pgType, Nullable`1 dbType, Action`1 configure, Boolean notNull)
   at Marten.MartenRegistry.DocumentMappingExpression`1.<>c__Displ
ayClass6_0.<Duplicate>b__0(DocumentMapping`1 mapping)
   at Marten.DocumentMappingBuilder`1.Build(StoreOptions options)
   at Marten.Storage.StorageFeatures.Build(Type type, StoreOptions options)
...

If I change the offending line:

.Duplicate(x => x.SessionId);

then I get an error about nullable (because public string? SessionId { get; set; })

and it's fixed if I write:

.Duplicate(x => x.SessionId!);

The MartenDb method signature looks like:

public MartenRegistry.DocumentMappingExpression<T> Duplicate(
    Expression<Func<T, object>> expression,
    ...)

First question: Am I correct in thinking that nullable doesn't apply in this context (from the error message)?

Second question: What are the C# rules around member field expressions and nullable? Can we construct a member field expression that has no sense of what nullable is.

CodePudding user response:

The only thing that matters whatsoever in the expression above is to identify members on your document type that will be translated under the covers to the Postgresql JSONB locator for that member. Nullable doesn't matter, and the coalescing operator usage has no applicability here. There's no other magic except the JSONB locator that matches the member (or member path if it's a deep locator).

So the only valid usage is .Duplicate(x => x.SessionId!);

Honestly not sure about the nullable usage of strings. That didn't even exist at the time the index creation code was built.

  • Related