Skip to content

Configured DbFunctionParameter doesn't impact query parameter mapping #25980

Open
@roji

Description

@roji

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).

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions