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

Assigning boolean in Select statement throws invalid cast. #2246

Closed
VeronicaWasson opened this issue May 21, 2015 · 8 comments
Closed

Assigning boolean in Select statement throws invalid cast. #2246

VeronicaWasson opened this issue May 21, 2015 · 8 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

@VeronicaWasson
Copy link

I have a DbContext like this:

public class BookContext : DbContext
{
    public DbSet<Author> Authors { get; set; }
 }

This code throws an invalid cast

var x = BookContext.Authors
    .Select(x => new
    {
        Text = x.FirstMidName,
        Value = x.AuthorID.ToString(),
        Selected = true  // Throws invalid cast
    }).ToList();

Works fine without 'Selected = true'.

Stack trace:

System.InvalidCastException was unhandled by user code
  HResult=-2147467262
  Message=Specified cast is not valid.
  Source=EntityFramework.Relational
  StackTrace:
       at Microsoft.Data.Entity.Relational.RelationalObjectArrayValueReader.ReadValue[T](Int32 index)
       at lambda_method(Closure , QuerySourceScope )
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at Microsoft.Data.Entity.Query.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at ContosoBooks.Controllers.BookController.GetAuthorsListItems(Int32 selected) in C:\Users\mwasson\Documents\Visual Studio 2015\Projects\sample\src\ContosoBooks\Controllers\BookController.cs:line 143
       at ContosoBooks.Controllers.BookController.Create() in C:\Users\mwasson\Documents\Visual Studio 2015\Projects\sample\src\ContosoBooks\Controllers\BookController.cs:line 45
  InnerException: 
@maumar
Copy link
Contributor

maumar commented Jun 4, 2015

Problem here is that we are sending the following query to the database:

SELECT [x].[FirstMidName], [x].[AuthorID], 1
FROM [Author] AS [x]

This then gets stored in the value buffer as {string, int, int}, rather than {string, int, bool} - hence the conversion fails, when we try to cast from int to bool.

Possible solution is to evaluate the constant on the client, rather than sending it to server. We need to make sure that we still translate constants that are bound to the source query somehow (part of a more complex expression involving a column, or an argument to a function)

@divega
Copy link
Contributor

divega commented Jun 5, 2015

Wouldn't CAST (1 AS BIT) work?

@maumar
Copy link
Contributor

maumar commented Jun 5, 2015

It would, and would be a simpler solution. However the other solution seems to be consistent with our funcletization strategy (funcletize as much as we can if its deterministic and doesn't bring more rows from the database).

@anpete
Copy link
Contributor

anpete commented Jun 5, 2015

Yes, better to do client eval here as it means less data on the wire.

@divega
Copy link
Contributor

divega commented Jun 5, 2015

Agreed, but any idea of how much work it would take each way? We need to be careful not to turn this little thing into something big 😄

@anpete
Copy link
Contributor

anpete commented Jun 5, 2015

Well, it shouldn't be too bad as we can already do the client eval. I will follow up with @maumar later today for an update.

@maumar
Copy link
Contributor

maumar commented Jun 5, 2015

I think we got a very easy/local fix for this

maumar added a commit that referenced this issue Jun 11, 2015
Problem is that a row projection containing a constant bool gets translated into integer (0 or 1) since 1 and 0 are the literal bit values for true and false. When we try to materialize, we try to cast integer 0 or 1 into bool, which causes an error.
Fix is to not send standalone constants to the store, but instead deal with them on the client. This is consistent with our approach to functions (funcletize as much as we can)
maumar added a commit that referenced this issue Jun 12, 2015
Problem is that a row projection containing a constant bool gets translated into integer (0 or 1) since 1 and 0 are the literal bit values for true and false. When we try to materialize, we try to cast integer 0 or 1 into bool, which causes an error.
Fix is to not send standalone constants to the store, but instead deal with them on the client. This is consistent with our approach to functions (funcletize as much as we can)
@maumar
Copy link
Contributor

maumar commented Jun 12, 2015

Fixed in e8e50c0

@maumar maumar closed this as completed Jun 12, 2015
@rowanmiller rowanmiller modified the milestones: 7.0.0, 7.0.0-beta6 Jul 15, 2015
@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-beta6, 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

6 participants