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

'Invalid cast from 'System.Int32' to 'efcore_cosmosdb_bug.OperationStatus'.' - EF Core Azure Cosmos DB Provider #20916

Closed
milannankov opened this issue May 11, 2020 · 3 comments · Fixed by #20934
Assignees
Labels
area-cosmos area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@milannankov
Copy link

milannankov commented May 11, 2020

I am using the EF Core Azure Cosmos DB Provider and trying to execute a query where I filter based on an enum value but I am getting "'Invalid cast from 'System.Int32' to 'efcore_cosmosdb_bug.OperationStatus'.'" error.

The problem is caused by the Status property of the class InitOperation.
There is also a conversion specified in the context but it does not seem to be helping.

The code that breaks is the following:

var result = await context.InitOperations
                .Where(_ => _.Status == OperationStatus.Completed)
                .ToListAsync();

Steps to reproduce

efcore-cosmosdb-bug.zip

I have attached a project that demostrates the problem.
Just run the CosmosDb emulator, add the connection settings and run the program.

Exception has occurred: CLR/System.InvalidCastException
An exception of type 'System.InvalidCastException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Invalid cast from 'System.Int32' to 'efcore_cosmosdb_bug.OperationStatus'.'
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.Int32.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType)
   at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.Sanitize[T](Object value)
   at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.<>c__DisplayClass3_0`2.<SanitizeConverter>b__0(Object v)
   at Microsoft.EntityFrameworkCore.Cosmos.Query.Internal.QuerySqlGenerator.GenerateJToken(Object value, CoreTypeMapping typeMapping)

Further technical details

EF Core version: 3.1.3
Database provider: 3.1.3
Target framework: .NET Core 3.1
Operating system: Windows 7
IDE: Visual Studio Code

@ralmsdeveloper
Copy link
Contributor

@ajcvickers
https://github.com/dotnet/efcore/blob/master/src/EFCore.Cosmos/Query/Internal/SelectExpression.cs#L143-L146

Should be?

_partitionKeyValueConverter = partitionKeyProperty.FindTypeMapping()?.Converter 
    ?? partitionKeyProperty.GetValueConverter();

@smitpatel
Copy link
Member

This code needs to be in core and more first class (Probably just be part of ValueConverter)

value = ConvertUnderlyingEnumValueToEnum(value);
if (Converter != null)
{
value = Converter.ConvertToProvider(value);
}

@milannankov
Copy link
Author

Is there a workaround on the framework level that can be used? There is nothing that I could think of besides making temporary changes to classes and directly use int instead of an Enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-cosmos area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants