Skip to content

Regression in v7.0.4: "Unable to cast object of type..." when using nullable primary key #30489

@bkoelman

Description

@bkoelman

After updating to EF Core v7.0.4, several of our tests are failing. What they have in common is a nullable private key. I suspect this is caused by #30219.

Include your code

The code below throws during SaveChanges(). Uncommenting the AppContext switch makes it print "Game:Id=0", which is the expected behavior.

using Microsoft.EntityFrameworkCore;

// AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue29985", true);

using (var dbContext = new ZeroKeyDbContext())
{
    dbContext.Database.EnsureDeleted();
    dbContext.Database.EnsureCreated();

    dbContext.Games.Add(new Game { Id = 0 });
    dbContext.SaveChanges();
}

using (var dbContext = new ZeroKeyDbContext())
{
    var game = dbContext.Games.First();
    Console.WriteLine($"Game:Id={game.Id}");
}

public sealed class Game
{
    public int? Id { get; set; }
}

public sealed class ZeroKeyDbContext : DbContext
{
    public DbSet<Game> Games => Set<Game>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(
            "Host=localhost;Port=5432;Database=Example;User ID=postgres;Password=postgres;Include Error Detail=true");
    }
}

Include stack traces

System.Reflection.TargetInvocationException
  HResult=0x80131604
  Message=Exception has been thrown by the target of an invocation.
  Source=System.Private.CoreLib
  StackTrace:
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.EntityFrameworkCore.Update.Internal.RowKeyValueFactoryFactory.Create(IUniqueConstraint key)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.UniqueConstraint.GetRowKeyValueFactory()
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.AddUniqueValueEdges()
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.TopologicalSort(IEnumerable`1 commands)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.<BatchCommands>d__9.MoveNext()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Program.<Main>$(String[] args) in D:\Bart\Source\Projects\EFCoreBug29985\EFCoreBug29985\Program.cs:line 11

  This exception was originally thrown at this call stack:

Inner Exception 1:
InvalidCastException: Unable to cast object of type 'System.Func`3[System.Nullable`1[System.Int32],System.Nullable`1[System.Int32],System.Boolean]' to type 'System.Func`3[System.Int32,System.Int32,System.Boolean]'.

Include provider and version information

EF Core version: 6.0.4
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: .NET 6.0
Operating system: Windows 11 Pro 22H2
IDE: Visual Studio 2022 17.4.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions