Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidCastException when Reading Encrypted Date Column in with Entity Framework #2274

Closed
EnderPi opened this issue Dec 18, 2023 · 1 comment · Fixed by #2275
Closed

InvalidCastException when Reading Encrypted Date Column in with Entity Framework #2274

EnderPi opened this issue Dec 18, 2023 · 1 comment · Fixed by #2275
Labels
🐛 Bug! Issues that are bugs in the drivers we maintain.

Comments

@EnderPi
Copy link

EnderPi commented Dec 18, 2023

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.

@JRahnama
Copy link
Contributor

cc @ErikEJ 🙏

ErikEJ added a commit to ErikEJ/SqlClient that referenced this issue Dec 18, 2023
ErikEJ added a commit to ErikEJ/SqlClient that referenced this issue Dec 18, 2023
@JRahnama JRahnama added the 🆕 Triage Needed For new issues, not triaged yet. label Dec 19, 2023
@kf-gonzalez kf-gonzalez added 🐛 Bug! Issues that are bugs in the drivers we maintain. and removed 🆕 Triage Needed For new issues, not triaged yet. labels Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug! Issues that are bugs in the drivers we maintain.
Projects
Development

Successfully merging a pull request may close this issue.

3 participants