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

Specified cast is not valid #4308

Closed
capesean opened this issue Jan 14, 2016 · 2 comments
Closed

Specified cast is not valid #4308

capesean opened this issue Jan 14, 2016 · 2 comments
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

@capesean
Copy link

In my example, I have a Time class with a Minutes field defined using the short data type.

If I try to get the total decimal hours like this:
var totalHours = _dbContext.Times.Sum(t => t.Minutes / 60m);

I get the error:

An exception of type 'System.InvalidCastException' occurred in EntityFramework.Core.dll but was not handled in user code. Additional information: Specified cast is not valid.

This, however, works fine:
var totalHours = _dbContext.Times.Sum(t => t.Minutes / 60);

@capesean
Copy link
Author

Can someone tell me if this is a bug? It just seems too simple to be an undocumented issue, but it's cropping up everywhere in my project.

@capesean
Copy link
Author

This seems to work:

var test = _dbContext.Times.Sum(t => (decimal?)Convert.ToDecimal(t.Minutes) / 60m);

I suppose it makes sense - you have to explicitly convert it to a decimal to get EF to do so. EF6 was able to infer this I guess. I need the (decimal?) in case there are no times, but that's not necessary for this particular issue. I just wanted to show that it looks a bit odd to have to cast twice. But I can live with that.

@rowanmiller rowanmiller added this to the 7.0.0 milestone Jan 15, 2016
@mikary mikary added the 2 - Done label Feb 2, 2016
@mikary mikary closed this as completed in 4001f15 Feb 23, 2016
@mikary mikary modified the milestones: 1.0.0-rc2, 1.0.0 Feb 23, 2016
@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

4 participants