Document advanced user-defined function mapping - #5459
Document advanced user-defined function mapping#5459AndriySvyryd with Copilot wants to merge 6 commits into
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Expands advanced EF Core user-defined function mapping documentation and samples.
Changes:
- Adds lambda registration, schema, nullability, and translation guidance.
- Demonstrates SQL Server
JSON_VALUEwith converted CLR types. - Clarifies
HasTranslationlimitations and nullability propagation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
Program.cs |
Adds the JSON function query. |
Model.cs |
Adds JSON entity, conversion, and function mapping. |
user-defined-function-mapping.md |
Documents advanced mapping APIs and behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
entity-framework/core/querying/user-defined-function-mapping.md:81
JsonEntityis required by the new example, but its declaration (and theJsonEntitiescontext property) is outside every referenced snippet, so readers cannot reproduce the configuration shown below from the page. Add a dedicated entity/context snippet before this paragraph, or include these declarations in an existing referenced region.
In this example, `JsonEntity.Metadata` is a dictionary stored as `nvarchar(max)` through a value converter. The `json` function parameter has the same store type, while the result uses the `nvarchar(4000)` type returned by `JSON_VALUE`:
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
entity-framework/core/querying/user-defined-function-mapping.md:122
- The translation sample immediately above violates this new requirement:
ABSreceives one SQL argument, butModel.cs:200supplies twoargumentsPropagateNullabilityentries. EF Core 10 requires these counts to match and throwsInvalidOperationExceptionwhenPercentageDifferenceis translated, so running the documented sample fails. Update that list to[true]while adding this caution.
> [!CAUTION]
> `HasTranslation` works with the SQL expression tree, not SQL text. The translation must construct valid <xref:Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlExpression> objects with the correct type mappings, nullability, and argument nullability propagation. Incorrect metadata can produce invalid SQL or incorrect query results, and the expression types used by a translation may be specific to a database provider. Use this low-level API only after understanding the provider's SQL expression tree; prefer a regular function mapping or an existing provider translation when possible.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
entity-framework/core/querying/user-defined-function-mapping.md:122
- The referenced
HasTranslationsample does not satisfy this new requirement: itsABSexpression has one SQL argument but passesargumentsPropagateNullability: [true, true].SqlFunctionExpressionrequires these counts to match, so executingquery2throwsInconsistentNumberOfArgumentsinstead of producing the SQL shown. Update that sample metadata to[true].
> `HasTranslation` works with the SQL expression tree, not SQL text. The translation must construct valid <xref:Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlExpression> objects with the correct type mappings, nullability, and argument nullability propagation. Incorrect metadata can produce invalid SQL or incorrect query results, and the expression types used by a translation may be specific to a database provider. Use this low-level API only after understanding the provider's SQL expression tree; prefer a regular function mapping or an existing provider translation when possible.
The user-defined function mapping guide omitted several registration, type-mapping, nullability, and translation APIs. It also lacked a practical built-in JSON function example.
Function registration
HasDbFunctionregistration and default arguments.DbFunctionAttribute, schemas, built-in functions, and nullability metadata.Store-type and JSON mapping
JSON_VALUEexample and clarify value-converter limitations.PropagatesNullabilityand link it to query null semantics.HasTranslationSQL expression trees.HasTranslation.