-
Notifications
You must be signed in to change notification settings - Fork 315
Closed
Description
Describe the bug
When reading from a "Date" column that utilizes column-level encryption in SQLServer using Entity Framework, an InvalidCastException occurs in SQLDataReader.cs, on the line "return (T)GetValueFromSqlBufferInternal(data, metaData);". The issue arises because the GetValueFromSqlBufferInternal call is returning a DateTime type instead of the expected DateOnly type, leading to the exception. The issue does not occur without column-level encryption.
Exception message:
System.InvalidCastException : Unable to cast object of type 'System.DateTime' to type 'System.DateOnly'
Stack trace:
at Microsoft.Data.SQLClient.SQLDataReader.GetFieldValueFromSqlBufferInternal[T](SQLBuffer data, _SqlMetadata metadata, Boolean isAsync)
at Microsoft.Data.SQLClient.SQLDataReader.GetFieldValueInternal[T](Int32 i, Boolean isAsync)
at Microsoft.Data.SQLClient.SQLDataReader.GetFieldValue[T](Int32 i)
at lambda_method153(Closure, QueryContext, DBDataReader, ResultContext, SingleQueryResultCoordinator)
To reproduce
public class EncryptedDate
{
public int Id { get; set; }
public DateOnly Dob { get; set; }
}
public class EFCoreTestContext : DbContext
{
public DbSet<EncryptedDate> EncryptedDates { get; set; }
}
static void Main()
{
/*Note - insert at least one value into the table before running this, and encrypt the Dob column to type "randomized" in SQLServer*/
using (var context = new EFCoreTestContext())
{
var date = context.EncryptedDates.First();
}
}
Expected behavior
Row reads correctly from the database.
Further technical details
Microsoft.Data.SqlClient version: 5.1.2
.NET target: .NET 8
SQL Server version: SQL Server 2019
Operating system: Windows 10 Pro
Additional context
Removing column-level encryption in SQL Server allows the above code to run correctly.
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
Closed