Repository navigation
Support sql_variant containing DateOnly as native SQL date without client conversion #3934
Description
Activity
@Harald-PH What error does it fail with?
We are using an EAV-aproach. The target field in database is variant null and the value is coming from a DateOnly Field.
I can workaround it by converting the DateOnly field into DateTime. But I am not sure about the consequences yet.If you can create a simple ADO.NET based repro I can have a look.
Which EF Core version?
Which Microsoft.Data.SqlClient version.
What does EAV approach mean?
If you can create a simple ADO.NET based repro I can have a look.
Which EF Core version?
Which Microsoft.Data.SqlClient version.
<PackageReference Include="EfCore.SchemaCompare" Version="10.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.2" />A sample project will take some time.
What does EAV approach mean?
(https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model)@Harald-PH I tried this simple console app, but could not make if fail (using Microsoft.Data.SqlClient 6.1,4) - can you modify to make it fail? If not, this could be an EF Core issue.
using Microsoft.Data.SqlClient; var connectionString = "Server=(localdb)\\mssqllocaldb;Database=Test;Integrated Security=true;Encrypt= false"; using var connection = new SqlConnection(connectionString); connection.Open(); var command = new SqlCommand(@" IF OBJECT_ID(N'[dbo].[Eav]', 'U') IS NULL BEGIN CREATE TABLE [dbo].[Eav] (Id INT PRIMARY KEY IDENTITY, Value sql_variant NULL) END ", connection); command.ExecuteNonQuery(); var insertCommand = new SqlCommand(@" INSERT INTO dbo.Eav (Value) VALUES (@value)", connection); insertCommand.Parameters.AddWithValue("@value", new DateOnly(2026, 2, 7)); insertCommand.ExecuteNonQuery();
var connectionString = "Server=(localdb)\\mssqllocaldb;Database=Test;Integrated Security=true;Encrypt= false"; using var connection = new SqlConnection(connectionString); connection.Open(); var command = new SqlCommand(@" IF OBJECT_ID(N'[dbo].[Eav]', 'U') is not null BEGIN drop table [dbo].[Eav] END CREATE TABLE [dbo].[Eav] (Id INT PRIMARY KEY IDENTITY, Value sql_variant NULL, Date date NULL) ", connection); command.ExecuteNonQuery(); var insertCommand = new SqlCommand(@" INSERT INTO dbo.Eav (Value,date) VALUES (@value, @value) ", connection); insertCommand.Parameters.AddWithValue("@value", new DateOnly(2026, 2, 7)); InsertCommand.ExecuteNonQuery();You can see that the value created is not a Date, but a DateTime.
I use EF Core Power Tools, and they probably assume that it is a Date and not a DateTime, which leads to the error I reported. You can work around this by using DateTime instead of DateOnly.
At this point, I cannot say whether I will encounter similar problems with my (future) system. That is why I wanted to point out this issue to you.Hi @Harald-PH - Thanks for submitting this issue. I will work with @ErikEJ to figure out what's going on.
Could you explain this statement for me?
You can see that the value created is not a Date, but a DateTime.
Which value are you referring to, where are you seeing the value created with the wrong type, and how can we verify your observations? Is the C# DateOnly being written to the table with an unexpected SQL data type? Is this happening with the
sql_variantcolumn, theDatecolumn, both? What SQL data type do you expect thesql_variantto actually be?I'll mark the issue as Waiting for Customer while you prepare your reply.
Reacted by Erik Ejlskov JensenI'm not entirely sure what the question is. But I'll try to answer it.
I expect the "Value" column to contain an object of the type of the object specified in the value clause. Without conversion!
In the above case, there is a value of the C# type DateOnly, which corresponds to the db type date.
When you run this code and look at the result in the database, you can see that a datetime object arrives in the database.
The other column ("Date") is for comparison purposes only.@Harald-PH When you say "look" please let us know what you mean exactly!
I believe he's saying that the sql_variant_property(value, 'basetype') is returning a datetime, not a date. Which implies that the boxed object DateOnly in the sql parameter is being sent with a sqldbtype of datetime.
Reacted by Erik Ejlskov Jensen and hhoyer-phs- moved this from Waiting for customer to Investigating in SqlClient Board
on Mar 4, 2026 More complete repro:
using Microsoft.Data.SqlClient; var connectionString = "Server=(localdb)\\mssqllocaldb;Database=Testproject;Integrated Security=true;Encrypt=false"; using var connection = new SqlConnection(connectionString); connection.Open(); using var command = new SqlCommand(@" IF OBJECT_ID(N'[dbo].[Eav]', 'U') IS NULL BEGIN CREATE TABLE [dbo].[Eav] ( Id INT PRIMARY KEY IDENTITY, VariantValue SQL_VARIANT NULL, DateValue DATE NULL) END ", connection); command.ExecuteNonQuery(); command.CommandText = @"DELETE FROM dbo.Eav"; command.ExecuteNonQuery(); var insertCommand = new SqlCommand(@" INSERT INTO dbo.Eav (VariantValue, DateValue) VALUES (@value, @value) ", connection); insertCommand.Parameters.AddWithValue("@value", new DateOnly(2026, 2, 7)); insertCommand.ExecuteNonQuery(); var selectCommand = new SqlCommand(@" SELECT VariantValue, DateValue FROM dbo.Eav ", connection); var reader = selectCommand.ExecuteReader(); reader.Read(); Console.WriteLine( $"VariantValue: {reader["VariantValue"]}, DateValue: {reader["DateValue"]}" ); var variantValueType = reader.GetFieldType(0); var dateValueType = reader.GetFieldType(1); reader.Close(); Console.WriteLine($"VariantValue Type: {variantValueType}, DateValue Type: {dateValueType}"); var variantValueBaseTypeCommand = new SqlCommand( @" SELECT SQL_VARIANT_PROPERTY(VariantValue, 'BaseType') AS BaseType FROM dbo.Eav ", connection); var baseType = variantValueBaseTypeCommand.ExecuteScalar(); Console.WriteLine($"VariantValue BaseType: {baseType}");
@paulmedynski Looks like this will be fixed with #4294
- moved this from Investigating to In progress in SqlClient Board
on Jul 11, 2026 has the submitted repro been verified? (See you closed this)
- added a commit that references this issue
on Sep 24, 2026
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Support sql_variant containing DateOnly as native SQL
datewithout client conversionProblem
When using a SQL Server column of type
sql_variantthat stores adate, and the CLR value is aDateOnly, the SqlClient does not treat it as a native SQLdate. Instead, it fails or requires manual conversion (e.g., viaDateOnly.ToDateTime()).Example:
Although SqlClient supports
DateOnlyfor direct parameters with the latest providers (5.1+), it does not automatically handlesql_variant:objectdateDateTimeExpected Behavior
DateOnlyas SQLdatewhen assigned to asql_variantcolumnvariantDateOnly→DateTimeconversion in application codeThis would align
sql_varianthandling forDateOnlywith other CLR types:Repro Steps
sql_variantcolumn.object?.DateOnlyvalue.DateTime.Additional Context
DateOnlyfor non-variant columnssql_variantis a generic SQL type, and automatic type inference is missingLabels (suggested)
enhancementarea/SqlClientneeds-investigation