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 for Nullable<DateTime> on Mono (RC1) #4012

Closed
mguinness opened this issue Dec 8, 2015 · 5 comments
Closed

InvalidCastException for Nullable<DateTime> on Mono (RC1) #4012

mguinness opened this issue Dec 8, 2015 · 5 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mguinness
Copy link

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 #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
Copy link
Author

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 DapperLib/Dapper#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')

@rowanmiller
Copy link
Contributor

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

@mguinness
Copy link
Author

@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 #3637.

@natemcmaster
Copy link
Contributor

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 natemcmaster changed the title InvalidCastException for Nullable<DateTime> in dnxcore50 but not dnx451 (RC1) InvalidCastException for Nullable<DateTime> on Mono (RC1) Jan 6, 2016
@robbpriestley
Copy link

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.

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants