Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

EF9 Cosmos RC2: .Where cannot be used with Enum conversion anymore on partition keys #34911

Closed
mwiedemeyer opened this issue Oct 15, 2024 · 4 comments
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 regression Servicing-consider type-bug
Milestone

Comments

@mwiedemeyer
Copy link

After updating from EF8 to EF9 (Microsoft.EntityFrameworkCore.Cosmos 9.0.0-rc.2.24474.1) the following code is no longer working

Code:

Entire sample console app here: https://github.com/mwiedemeyer/EF9EnumConversionBug/blob/main/ConsoleApp1/Program.cs

// setup 
// (...)
memberModel.Property(p => p.MemberType).HasConversion(v => v.ToString(), v => Enum.Parse<MemberType>(v, true));

// this is working as expected. The conversion from string to enum is working
var allMembers = await dbContext.Members.ToListAsync();
// This is not working. The conversion from enum to string is not working
// EXCEPTION: 'Invalid cast from 'System.Int32' to 'MemberType'
var adminMembers = await dbContext.Members.Where(p => p.MemberType == MemberType.Admin).ToListAsync();

Stack Trace

System.InvalidCastException: 'Invalid cast from 'System.Int32' to 'MemberType'.'
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.Sanitize[T](Object value)
   at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.<>c__DisplayClass7_0`2.<SanitizeConverter>b__1(Object v)
   at Microsoft.EntityFrameworkCore.Internal.PartitionKeyBuilderExtensions.Add(PartitionKeyBuilder builder, Object value, IProperty property)
   at Microsoft.EntityFrameworkCore.Cosmos.Query.Internal.CosmosShapedQueryCompilingExpressionVisitor.GeneratePartitionKey(IEntityType rootEntityType, List`1 partitionKeyPropertyValues, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Cosmos.Query.Internal.CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable`1..ctor(CosmosQueryContext cosmosQueryContext, ISqlExpressionFactory sqlExpressionFactory, IQuerySqlGeneratorFactory querySqlGeneratorFactory, SelectExpression selectExpression, Func`3 shaper, Type contextType, IEntityType rootEntityType, List`1 partitionKeyPropertyValues, Boolean standAloneStateManager, Boolean threadSafetyChecksEnabled)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__67`1.MoveNext()
   at Program.<<Main>$>d__0.MoveNext() in X:\Dev\ConsoleApp1\ConsoleApp1\Program.cs:line 36

EF Core version: 9.0.0-rc.2.24474.1
Database provider: Microsoft.EntityFrameworkCore.Cosmos
Target framework: .NET 9 RC2

@roji
Copy link
Member

roji commented Oct 16, 2024

Confirmed regresion in 9.0.

@roji roji self-assigned this Oct 16, 2024
roji added a commit to roji/efcore that referenced this issue Oct 16, 2024
roji added a commit to roji/efcore that referenced this issue Oct 16, 2024
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 16, 2024
@roji roji added this to the 9.0.0 milestone Oct 16, 2024
@roji roji changed the title EF9 Cosmos RC2: .Where cannot be used with Enum conversion anymore EF9 Cosmos RC2: .Where cannot be used with Enum conversion anymore on partition keys Oct 16, 2024
@roji
Copy link
Member

roji commented Oct 16, 2024

Thanks for flagging this @mwiedemeyer; FYI the bug is specific to partition key properties only (regular enum properties work fine).

@mwiedemeyer
Copy link
Author

Ah thanks. In my case I can switch to a regular property for now but I hope that will be fixed later.

@roji
Copy link
Member

roji commented Oct 16, 2024

Absolutely - although this is very late in the release, we'll do our best to fix this for 9.0.0.

# for free to join this conversation on GitHub. Already have an account? # 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 regression Servicing-consider type-bug
Projects
None yet
Development

No branches or pull requests

2 participants