You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using Microsoft.EntityFrameworkCore;using System;using System.Linq;using System.Threading.Tasks;namespaceEfCoreBoolToDateIssue{publicclassBlog{publicintId{get;set;}publicstringTitle{get;set;}publicboolRemoved{get;set;}}publicclassBlogContext:DbContext{publicDbSet<Blog> Blog {get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=> optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EfCoreBoolToDateIssue;Trusted_Connection=True;ConnectRetryCount=0").LogTo(message => Console.WriteLine(message));protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){}}publicstaticclassProgram{staticasync Task Main(string[]args){using(varcontext=new BlogContext()){
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Blog
{Title="Hello world",Removed=true});
context.SaveChanges();}using(varcontext=new BlogContext()){varblogs=await context.Blog.Select(b =>new{ Title = b.Title,// Next line will trigger System.InvalidCastException:// 'Unable to cast object of type 'System.String' to type 'System.DateTime'.'//// This is the generated SQL:// SELECT[b].[Title],// CASE// WHEN[b].[Removed] = CAST(1 AS bit) THEN '2000-01-01T00:00:00.0000000'// ELSE '1900-01-01T00:00:00.0000000'// END AS[RemoveDate]// FROM[Blog] AS[b] RemoveDate = b.Removed ?new DateTime(2000,1,1):new DateTime(1900,1,1)// This is a workaround// RemoveDate = DateTime.Parse(b.Removed ? "2000-01-01" : "1900-01-01")}).ToListAsync();}}}}
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.DateTime'.
at Microsoft.Data.SqlClient.SqlBuffer.get_DateTime() in Microsoft.Data.SqlClient.dll:token 0x6000a48+0x76
at Microsoft.Data.SqlClient.SqlDataReader.GetDateTime(Int32 i) in Microsoft.Data.SqlClient.dll:token 0x6000d39+0x9
at lambda_method39(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync() in Microsoft.EntityFrameworkCore.Relational.dll:token 0x6001b0f+0x1aa
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken) in Microsoft.EntityFrameworkCore.dll:token 0x6000214+0xcc
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken) in Microsoft.EntityFrameworkCore.dll:token 0x6000214+0x159
at EfCoreBoolToDateIssue.Program.Main(String[] args) in C:\Users\msn\source\repos\EfCoreBoolToDateTimeIssue\EfCoreBoolToDateTimeIssue\Program.cs:line 51
at EfCoreBoolToDateIssue.Program.<Main>(String[] args) in EfCoreBoolToDateTimeIssue.dll:token 0x6000014+0xc
Include provider and version information
EF Core version: 6.0.0-preview.6.21352.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
IDE: Command line
Also tried in .NET 5 and EF Core 5 with the same result.
The text was updated successfully, but these errors were encountered:
File a bug
"Removed" is a bool in the Blog-entity, this line will throw System.InvalidCastException.
Maybe this is the same as: #24075
Sample code
Project file
Stack trace
Include provider and version information
EF Core version: 6.0.0-preview.6.21352.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
IDE: Command line
Also tried in .NET 5 and EF Core 5 with the same result.
The text was updated successfully, but these errors were encountered: