dotnet / dotnet/efcore

Add easy support for user-defined niladic functions

Open
#33,817 0 comments 0 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

We allow users to map arbitrary user functions, but support is missing for niladic ones. The example raised by @Coder3333 [here](https://github.com/dotnet/efcore/issues/32969#issuecomment-2127641343) was for `CURRENT_USER` (note no parentheses).

This can be worked around via manual SQL translation to SqlFunctionExpression:

```c#
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDbFunction(typeof(BlogContext).GetMethod(nameof(CurrentUser), [])!)
.HasTranslation(_ =>
new SqlFunctionExpression("CURRENT_USER", nullable: false, typeof(string), typeMapping: null));
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.