efcore icon indicating copy to clipboard operation
efcore copied to clipboard

InvalidCastException for Nullable<DateTime> on Mono (RC1)

Open mguinness opened this issue 10 years ago • 5 comments

Getting an InvalidCastException when using EntityFramework.MicrosoftSqlServer (7.0.0-rc1-final) but only in Core-CLR not Full-CLR.

Column definition is: [ApprovedDate] [datetime2](7) NULL

Class definition is: public DateTime? ApprovedDate { get; set; }

I found a similar issue at https://github.com/aspnet/EntityFramework/issues/2596 but unsure if it's related.

Full error shown below:

Microsoft.Data.Entity.Query.Internal.QueryCompiler: An exception occurred in the database while iterating the results of a query. System.InvalidCastException: Cannot cast from source type to destination type. at System.Nullable'1<System.DateTime>.Unbox (object) <0x00060> at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,Microsoft.Data.Entity.Storage.ValueBuffer) <0x000cd> at Microsoft.Data.Entity.Query.EntityLoadInfo.Materialize () <0x00037> at Microsoft.Data.Entity.Query.Internal.QueryBuffer.GetEntity (Microsoft.Data.Entity.Infrastructure.IKeyValue,Microsoft.Data.Entity.Query.EntityLoadInfo,bool) <0x000c3> at Microsoft.Data.Entity.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.CreateEntity<Portal.Models.Content> (Remotion.Linq.Clauses.IQuerySource,Microsoft.Data.Entity.Storage.ValueBuffer,int,Microsoft.Data.Entity.Query.QueryContext,Microsoft.Data.Entity.Metadata.IEntityType,bool,Microsoft.Data.Entity.ChangeTracking.Internal.KeyValueFactory,System.Func'2<Microsoft.Data.Entity.Storage.ValueBuffer, object>,bool,bool) <0x0016a> at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,Microsoft.Data.Entity.Storage.ValueBuffer) <0x001ee> at System.Linq.Enumerable/<CreateSelectIterator>c__Iterator10'2<Microsoft.Data.Entity.Storage.ValueBuffer, Portal.Models.Content>.MoveNext () <0x0017e> at System.Linq.Enumerable.First<Portal.Models.Content> (System.Collections.Generic.IEnumerable'1<Portal.Models.Content>,System.Func'2<Portal.Models.Content, bool>,System.Linq.Enumerable/Fallback) <0x000af> at System.Linq.Enumerable.FirstOrDefault<Portal.Models.Content> (System.Collections.Generic.IEnumerable'1<Portal.Models.Content>) <0x0005f> at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,Microsoft.Data.Entity.Query.QueryContext) <0x00286> at Microsoft.Data.Entity.Query.Internal.QueryCompiler/<>c__DisplayClass18_1'1<Portal.Models.Content>.<CompileQuery>b__1 (Microsoft.Data.Entity.Query.QueryContext) <0x00033>

mguinness avatar Dec 08 '15 22:12 mguinness

I've been digging into this a bit more. It appears that this is an issue w/ datetime2 and not datetime, the latter has no issues. Also if I set the value to null it works and it only breaks when non-null.

Even though this is for Dapper and not EF, a similar issue is reported at https://github.com/StackExchange/dapper-dot-net/issues/295 (also MySQL not MSSQL).

Attached is a simple repro WebApplication5.docx (rename to zip). Below is the SQL you can use to add to a database. You will need to change the connection string in Startup.cs also.

CREATE TABLE [dbo].[Content] (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ApprovedDate] [datetime2](7) NULL
    CONSTRAINT [PK_Content] PRIMARY KEY CLUSTERED ([Id])
)

INSERT INTO Content VALUES ('2015-11-28 12:04:24.9077591')

mguinness avatar Dec 10 '15 02:12 mguinness

@mguinness your project works for me on Windows, are you running on OSX or Linux?

rowanmiller avatar Dec 16 '15 17:12 rowanmiller

@rowanmiller - correct, I'm running it against the docker image microsoft/aspnet. I'm only currently able to run it against the Mono based image, not the Core-CLR image due to the issue reported at https://github.com/aspnet/EntityFramework/issues/3637.

mguinness avatar Dec 17 '15 22:12 mguinness

This is a duplicate of #2596

@mguinness this is a bug in Mono, not CoreCLR. Your project passed just fine for me on Linux/OSX on CoreCLR (with workarounds for bugs that will be fixed in RC2)

natemcmaster avatar Jan 06 '16 19:01 natemcmaster

Could anyone in the know do me a favour and take a quick look at my issue reported at http://stackoverflow.com/questions/36192263/ef7-datetime-specified-cast-is-not-valid and let me know if you think my issue is related to this bug? Although my issue is not a 100% match as reported here, it certainly seems to be similar as it relates to DateTime types in EF7 using mono on OSX and a migration to SQL Server.

wildforestnorth avatar Mar 25 '16 01:03 wildforestnorth