Skip to content

ExecuteSql* methods should allow nullable object? parameters #35813

Closed as not planned
@jekingry

Description

@jekingry

Bug description

Example:

int? value = null;

// this will not compile
dbContext.Database.ExecuteSqlRaw("UPDATE SomeTable SET Value = {0}", value);

// This will throw The current provider doesn't have a store type mapping for properties of type 'DBNull'. 
dbContext.Database.ExecuteSqlRaw("UPDATE SomeTable SET Value = {0}", (object?)value ?? DBNull.Value);

// This is the only option to get the intended behavior
dbContext.Database.ExecuteSqlRaw("UPDATE SomeTable SET Value = {0}", value!);

So there is handling for converting null values to DBNull.Value, so shouldn't parameters allow null values?

Your code

int? value = null;

// this will not compile
dbContext.Database.ExecuteSqlRaw("UPDATE SomeTable SET Value = {0}", value);

// This will throw The current provider doesn't have a store type mapping for properties of type 'DBNull'. 
dbContext.Database.ExecuteSqlRaw("UPDATE SomeTable SET Value = {0}", (object?)value ?? DBNull.Value);

// This is the only option to get the intended behavior
dbContext.Database.ExecuteSqlRaw("UPDATE SomeTable SET Value = {0}", value!);

Stack traces

Error Message: System.InvalidOperationException : The current provider doesn't have a store type mapping for properties of type 'DBN
      ull'.
      Stack Trace:
         at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource, Type clrType, IModel model)
         at Microsoft.EntityFrameworkCore.Storage.Internal.RawSqlCommandBuilder.Build(String sql, IEnumerable`1 parameters, IModel model)
         at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlRawAsync(DatabaseFacade databaseFacade, String sql, IEnumerable`1 parameters, CancellationToken cancellationToken)   
         at IntegrationTests.ExampleTest.TestQuery() in C:\IntegrationTests\ExampleTest.cs:line 30

Verbose output


EF Core version

9.0.1

Database provider

MSSQL

Target framework

9.0.201

Operating system

Windows 11

IDE

VS Code

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions