Configured DbFunctionParameter doesn't impact query parameter mapping
@AndriySvyryd is already working on this.
Since Sep 14, 2021.
Assessment
This issue has not been assessed yet.
Description
When configuring a function's parameter's mapping in the model:
modelBuilder.HasDbFunction(...).HasParameter("startDate").Metadata.TypeMapping
= typeMappingSource.GetMapping("...");
My expectation was that this would impact the mapping used in the query pipeline (i.e. when a constant/parameter is given), but that does not seem to be the case.
Repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
await ctx.Database.ExecuteSqlRawAsync(@"
CREATE FUNCTION Foo(@p bit)
RETURNS int
AS
BEGIN
RETURN 0;
END");
_ = await ctx.Blogs.Where(b => ctx.Foo("true") == 0).ToListAsync();
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
static ILoggerFactory ContextLoggerFactory
=> LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0")
.EnableSensitiveDataLogging()
.UseLoggerFactory(ContextLoggerFactory);
public int Foo(object p) => throw new NotSupportedException();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var typeMappingSource = this.GetService<IRelationalTypeMappingSource>();
modelBuilder.HasDbFunction(typeof(BlogContext).GetMethod(nameof(Foo))!)
.HasParameter("p")
.Metadata.TypeMapping = typeMappingSource.GetMapping(typeof(bool));
}
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
}
The above generates the SQL [dbo].[Foo](N'true'), whereas I'd expect it to generate [dbo].[Foo](CAST(1 AS bit)) (or possibly throw).
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 134
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100