-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Constructor not found for type 'Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableClassCurrentProviderValueComparer #32411
Comments
Please share your .csproj at least. |
Here is a slightly cleaned-up csproj file At this point it's a bit inpractical for me to post a complete repro - was hoping for a quick insight what might be the issue, maybe indeed it's something about dependencies. When needed I will work on isolating the problem further. |
Having the same issue when upgrading from net6 to net8 |
This kind of issue is almost always the result of mixing an old version of an EF provider with a new version of EF itself (or vice versa). @kskalski I can't see something immediately wrong with the csproj you posted, but that's just an isolated csproj and is incomplete (i.e. there may be dependencies doing something there). I'd recommend looking at the entire dependency graph after If you can't figure it out, then yes, we'll need a minimal, runnable code sample that shows the problem in order to investigate. |
Or use |
Didn't know that one, thanks @ErikEJ! |
The transitive package list is below. I can see some packages held by at 7, so this might be related... will investigate some of those deps later on
|
@kskalski There is nothing obviously wrong with your references, and I am unable to reproduce what you are seeing with the snippets posted, so can you please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
It appears this structure of models triggers the error: class Key {
public string Str() { return ""; }
public static Key From(byte[] b) { return new Key(); }
}
class Parent {
public Key Guid { get; set; }
}
class Child {
public Key ParentGuid { get; set; }
public long Id { get; set; }
}
public class ApplicationDbContext : IdentityDbContext {
static readonly Expression<Func<Key, string>> KEY_STR_EXPR = v => v.Str();
static readonly Expression<Func<string, Key>> STR_KEY_EXPR = v => Key.From(Convert.FromBase64String(v));
static readonly ValueComparer<Key> KEY_COMPARER = new ValueComparer<Key>(
(a, b) => b != null && a.Equals(b), a => a.GetHashCode());
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.UseIdentityByDefaultColumns();
modelBuilder.Entity<Parent>(b => {
b.Property(e => e.Guid).HasConversion(KEY_STR_EXPR, STR_KEY_EXPR, KEY_COMPARER);
b.HasKey(e => e.Guid);
});
modelBuilder.Entity<Child>(b => {
b.HasOne<Parent>().WithMany().HasForeignKey(e => e.ParentGuid).IsRequired();
b.HasKey("ParentGuid", "Id");
});
}
} |
@kskalski I am not able to reproduce this--see my code below. This code requires only the following NuGet packages: <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" /> Does the code run for you with just these packages? If so, I would start triangulating the difference between this and the code that fails. using (var context = new ApplicationDbContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
}
class Key
{
public string Str()
{
return "";
}
public static Key From(byte[] b)
{
return new Key();
}
}
class Parent
{
public Key Guid { get; set; }
}
class Child
{
public Key ParentGuid { get; set; }
public long Id { get; set; }
}
public class ApplicationDbContext : IdentityDbContext
{
static readonly Expression<Func<Key, string>> KEY_STR_EXPR = v => v.Str();
static readonly Expression<Func<string, Key>> STR_KEY_EXPR = v => Key.From(Convert.FromBase64String(v));
static readonly ValueComparer<Key> KEY_COMPARER = new ValueComparer<Key>(
(a, b) => b != null && a.Equals(b), a => a.GetHashCode());
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql("MyConnectionString")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.UseIdentityByDefaultColumns();
modelBuilder.Entity<Parent>(b =>
{
b.Property(e => e.Guid).HasConversion(KEY_STR_EXPR, STR_KEY_EXPR, KEY_COMPARER);
b.HasKey(e => e.Guid);
});
modelBuilder.Entity<Child>(b =>
{
b.HasOne<Parent>().WithMany().HasForeignKey(e => e.ParentGuid).IsRequired();
b.HasKey("ParentGuid", "Id");
});
}
} |
Ah, right, Here is a complete project that reproduces bug for me: |
…t type as principal properties Fixes #32411 The issue here is that when a value converter is applied to a principal property, then that converter is used by the dependent properties unless something else is explicitly configured. However, this meant that when the PK has a converter but the FK does not, then the FK can get configured as a primitive collection, since it doesn't have a converter preventing this. The fix is to add a convention that sets the element type for dependent properties to match that for principal properties.
…t type as principal properties Fixes #32411 The issue here is that when a value converter is applied to a principal property, then that converter is used by the dependent properties unless something else is explicitly configured. However, this meant that when the PK has a converter but the FK does not, then the FK can get configured as a primitive collection, since it doesn't have a converter preventing this. The fix is to add a convention that sets the element type for dependent properties to match that for principal properties.
…t type as principal properties (#32560) * Ensure that, by convention, dependent properties have the same element type as principal properties Fixes #32411 The issue here is that when a value converter is applied to a principal property, then that converter is used by the dependent properties unless something else is explicitly configured. However, this meant that when the PK has a converter but the FK does not, then the FK can get configured as a primitive collection, since it doesn't have a converter preventing this. The fix is to add a convention that sets the element type for dependent properties to match that for principal properties. * Update based on review
…t type as principal properties (#32560) * Ensure that, by convention, dependent properties have the same element type as principal properties Fixes #32411 The issue here is that when a value converter is applied to a principal property, then that converter is used by the dependent properties unless something else is explicitly configured. However, this meant that when the PK has a converter but the FK does not, then the FK can get configured as a primitive collection, since it doesn't have a converter preventing this. The fix is to add a convention that sets the element type for dependent properties to match that for principal properties. * Update based on review
I'm trying to migrate from .NET Core 7 to .NET Core 8 and EF fails to validate my model throwing exception
Include your code
Since the exception relates to comparers I suppose it might be related to one of my custom comparers like:
Include stack traces
I get this exception on startup:
Include provider and version information
EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.Sqlite (8.0.0) / Npgsql.EntityFrameworkCore.PostgreSQL (8.0.0)
Target framework: .NET 8
Operating system: Windows 11
The text was updated successfully, but these errors were encountered: