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

InvalidCastException when doing a batch update on Npgsql-based context using a SelectListIterator to filter #804

Closed
mennomacomi opened this issue Jul 17, 2024 · 3 comments
Assignees

Comments

@mennomacomi
Copy link

Here is what to include in your request to make sure we implement a solution as quickly as possible.

1. Description

When using an Npgsql-based database context, running a batch update query on a collection filtered through a SelectListIterator throws a System.InvalidCastException

Example code:

class Model 
{
  public int Id { get; set; }
  public string Foo { get; set; }
}

class DatabaseContext : DbContext
{
  public DbSet<Model> Models { get; set; }
  public DatabaseContext(DbContextOptions options) : base(options) { }
}

class Program 
{
  public static void Main(string[] args) 
  {
    using var context = new DbContext(new DbContextOptionsBuilder<DatabaseContext>().UseNpgsql("<ConnectionString>").Options);
    context.Models.Add(new Model() { Id = 1, Foo = "foo" });
    context.SaveChanges();

    var idList = new List<int>() { 1 }
    // Doesn't have a real purpose of course, but for example's sake. The point is that idsToUpdate is a SelectListIterator
    var idsToUpdate = idList.Select(x => x);
    // Throws InvalidCastException
    context.Models.Where(x => idsToUpdate.Contains(x.Id)).Update(x => new() { Foo = "bar" });
  }
}

When changing .Where(x => idsToUpdate.Contains(x.Id)) to .Where(x => idList.Contains(x.Id)) it does work
When using another provider (tested with InMemory and Sqlite) it does work

2. Exception

If you are seeing an exception, include the full exception details (message and stack trace).

Exception message:
System.InvalidCastException : Unable to cast object of type 'SelectListIterator`2[System.Int32, System.Int32]' to type 'System.Collections.Generic.IList`1[System.Int32]'.
Stack trace:
 lambda_method469(Closure, IEnumerable`1)
 <>c__DisplayClass6_0`2.<SanitizeConverter>b__1(Object v)
 RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
 MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)

3. Fiddle or Project

See example code above

4. Any further technical details

This was working fine in version 6.100.0.5 but is broken in the latest version

Further technical details

  • EF version: [EF Core v8.0.7]
  • EF Plus version: [EF Plus Core v8.103.0]
  • Database Server version: [PostgreSQL 14]
  • Database Provider version (NuGet): [Npgsql.EntityFrameworkCore.PostgreSQL v8.0.4]
@JonathanMagnan JonathanMagnan self-assigned this Jul 18, 2024
@JonathanMagnan
Copy link
Member

Hello @mennomacomi ,

Thank you for reporting.

We will look at it.

Best Regards,

Jon

@JonathanMagnan
Copy link
Member

Hello @mennomacomi ,

The v8.103.1.0 has been released today.

Let us know if the new version has fixed your issue correctly.

Best Regards,

Jon

@mennomacomi
Copy link
Author

Hi @JonathanMagnan

I can confirm the new version has fixed the issue.
Thanks so much for the quick response and fix!

Kind regards,

Menno

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants