Skip to content

Regression between v9 and v10: System.InvalidCastException when query parameter is IEnumerable #38008

@nlriverty

Description

@nlriverty

Bug description

In version 9 if an IEnumerable is used as part of the query it succeeds. In version 10 it fails with an exception. The workaround is to materialize it to an array.

Your code

namespace BugRepro.Entities;

public class Attachment
{
    public int Id { get; set; }
    public MalwareStatus MalwareStatus { get; set; }
}
[Flags]
public enum MalwareStatus
{
    Clean = 1,
    Malware = 2
}

namespace BugRepro.ViewModel;

[Flags]
public enum MalwareStatus
{
    Clean = 1,
    Malware = 2
}

var filter = new[] { BugRepro.ViewModel.MalwareStatus.Malware }.Cast<MalwareStatus>(); // ToArray() fixes it
var fromDb = db.Attachments.Where(a => filter.Any(f => (f & a.MalwareStatus) != 0))
    .Select(a => new
    {
        Status = (BugRepro.ViewModel.MalwareStatus)a.MalwareStatus
    })
    .AsAsyncEnumerable();

await foreach (var item in fromDb)
{
    Console.WriteLine(item);
}

Stack traces

System.InvalidCastException: Invalid cast from 'BugRepro.ViewModel.MalwareStatus' to 'BugRepro.Entities.MalwareStatus'.
   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__DisplayClass2_0`2.<SanitizeConverter>b__1(Object v)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping.CreateParameter(DbCommand command, String name, Object value, Nullable`1 nullable, ParameterDirection direction)
   at Microsoft.EntityFrameworkCore.Storage.Internal.TypeMappedRelationalParameter.AddDbParameter(DbCommand command, Object value)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBase.AddDbParameter(DbCommand command, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateDbCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
   at Program.<Main>$(String[] args) in C:\git\efcore-10-bug-repro\BugRepro\BugRepro\Program.cs:line 34
   at Program.<Main>$(String[] args) in C:\git\efcore-10-bug-repro\BugRepro\BugRepro\Program.cs:line 34
   at Program.<Main>$(String[] args) in C:\git\efcore-10-bug-repro\BugRepro\BugRepro\Program.cs:line 34
   at Program.<Main>(String[] args)

Verbose output


EF Core version

10.0.5

Database provider

SqlServer

Target framework

.NET 10

Operating system

Windows

IDE

Rider

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions