Closed kskalski closed 10 months ago
Please share your .csproj at least.
Here is a slightly cleaned-up csproj file EFBug.csproj.gz
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 dotnet restore
(e.g. via obj/project.assets.json
).
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 dotnet list package --include-transitive
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
[net8.0]:
Pakiet najwyższego poziomu Żądane Rozpoznane
> log4net 2.0.15 2.0.15
> Microsoft.AspNetCore.Identity.EntityFrameworkCore 8.0.0 8.0.0
> Microsoft.AspNetCore.Mvc.NewtonsoftJson 8.0.0 8.0.0
> Microsoft.Bcl.AsyncInterfaces 8.0.0 8.0.0
> Microsoft.EntityFrameworkCore.Design 8.0.0 8.0.0
> Microsoft.EntityFrameworkCore.Sqlite 8.0.0 8.0.0
> Microsoft.Extensions.Localization 8.0.0 8.0.0
> Microsoft.Extensions.Logging.Log4Net.AspNetCore 7.0.0 7.0.0
> Microsoft.VisualStudio.Web.CodeGeneration.Design 8.0.0 8.0.0
> Npgsql.EntityFrameworkCore.PostgreSQL 8.0.0 8.0.0
> prometheus-net.AspNetCore 8.1.0 8.1.0
> System.Text.Encoding.CodePages 8.0.0 8.0.0
> VueCliMiddleware 6.0.0 6.0.0
Pakiet przechodni Rozpoznane
> FFMediaToolkit 4.4.1
> FFmpeg.AutoGen 5.0.0
> Google.Api.CommonProtos 2.5.0
> Google.Api.Gax 3.7.0
> Google.Api.Gax.Grpc 3.7.0
> Google.Api.Gax.Grpc.GrpcCore 3.7.0
> Google.Apis 1.57.0
> Google.Apis.Auth 1.57.0
> Google.Apis.Core 1.57.0
> Google.Cloud.Speech.V1 2.9.0
> Google.LongRunning 2.3.0
> Google.Protobuf 3.24.3
> Grpc.Auth 2.46.6
> Grpc.Core 2.46.6
> Grpc.Core.Api 2.46.6
> Humanizer 2.14.1
> Humanizer.Core 2.14.1
> Humanizer.Core.af 2.14.1
> Humanizer.Core.ar 2.14.1
> Humanizer.Core.az 2.14.1
> Humanizer.Core.bg 2.14.1
> Humanizer.Core.bn-BD 2.14.1
> Humanizer.Core.cs 2.14.1
> Humanizer.Core.da 2.14.1
> Humanizer.Core.de 2.14.1
> Humanizer.Core.el 2.14.1
> Humanizer.Core.es 2.14.1
> Humanizer.Core.fa 2.14.1
> Humanizer.Core.fi-FI 2.14.1
> Humanizer.Core.fr 2.14.1
> Humanizer.Core.fr-BE 2.14.1
> Humanizer.Core.he 2.14.1
> Humanizer.Core.hr 2.14.1
> Humanizer.Core.hu 2.14.1
> Humanizer.Core.hy 2.14.1
> Humanizer.Core.id 2.14.1
> Humanizer.Core.is 2.14.1
> Humanizer.Core.it 2.14.1
> Humanizer.Core.ja 2.14.1
> Humanizer.Core.ko-KR 2.14.1
> Humanizer.Core.ku 2.14.1
> Humanizer.Core.lv 2.14.1
> Humanizer.Core.ms-MY 2.14.1
> Humanizer.Core.mt 2.14.1
> Humanizer.Core.nb 2.14.1
> Humanizer.Core.nb-NO 2.14.1
> Humanizer.Core.nl 2.14.1
> Humanizer.Core.pl 2.14.1
> Humanizer.Core.pt 2.14.1
> Humanizer.Core.ro 2.14.1
> Humanizer.Core.ru 2.14.1
> Humanizer.Core.sk 2.14.1
> Humanizer.Core.sl 2.14.1
> Humanizer.Core.sr 2.14.1
> Humanizer.Core.sr-Latn 2.14.1
> Humanizer.Core.sv 2.14.1
> Humanizer.Core.th-TH 2.14.1
> Humanizer.Core.tr 2.14.1
> Humanizer.Core.uk 2.14.1
> Humanizer.Core.uz-Cyrl-UZ 2.14.1
> Humanizer.Core.uz-Latn-UZ 2.14.1
> Humanizer.Core.vi 2.14.1
> Humanizer.Core.zh-CN 2.14.1
> Humanizer.Core.zh-Hans 2.14.1
> Humanizer.Core.zh-Hant 2.14.1
> Microsoft.AspNetCore.Cryptography.Internal 8.0.0
> Microsoft.AspNetCore.Cryptography.KeyDerivation 8.0.0
> Microsoft.AspNetCore.JsonPatch 8.0.0
> Microsoft.AspNetCore.Razor.Language 6.0.24
> Microsoft.AspNetCore.SpaServices.Extensions 6.0.0
> Microsoft.Build 17.7.2
> Microsoft.Build.Framework 17.7.2
> Microsoft.CodeAnalysis.Analyzers 3.3.4
> Microsoft.CodeAnalysis.AnalyzerUtilities 3.3.0
> Microsoft.CodeAnalysis.Common 4.8.0-3.final
> Microsoft.CodeAnalysis.CSharp 4.8.0-3.final
> Microsoft.CodeAnalysis.CSharp.Features 4.8.0-3.final
> Microsoft.CodeAnalysis.CSharp.Workspaces 4.8.0-3.final
> Microsoft.CodeAnalysis.Elfie 1.0.0
> Microsoft.CodeAnalysis.Features 4.8.0-3.final
> Microsoft.CodeAnalysis.Razor 6.0.24
> Microsoft.CodeAnalysis.Scripting.Common 4.8.0-3.final
> Microsoft.CodeAnalysis.Workspaces.Common 4.8.0-3.final
> Microsoft.CSharp 4.7.0
> Microsoft.Data.Sqlite.Core 8.0.0
> Microsoft.DiaSymReader 2.0.0
> Microsoft.DotNet.Scaffolding.Shared 8.0.0
> Microsoft.EntityFrameworkCore 8.0.0
> Microsoft.EntityFrameworkCore.Abstractions 8.0.0
> Microsoft.EntityFrameworkCore.Analyzers 8.0.0
> Microsoft.EntityFrameworkCore.Relational 8.0.0
> Microsoft.EntityFrameworkCore.Sqlite.Core 8.0.0
> Microsoft.Extensions.Caching.Abstractions 8.0.0
> Microsoft.Extensions.Caching.Memory 8.0.0
> Microsoft.Extensions.Configuration 7.0.0
> Microsoft.Extensions.Configuration.Abstractions 8.0.0
> Microsoft.Extensions.Configuration.Binder 7.0.4
> Microsoft.Extensions.DependencyInjection 8.0.0
> Microsoft.Extensions.DependencyInjection.Abstractions 8.0.0
> Microsoft.Extensions.DependencyModel 8.0.0
> Microsoft.Extensions.FileProviders.Abstractions 6.0.0
> Microsoft.Extensions.FileProviders.Physical 6.0.0
> Microsoft.Extensions.FileSystemGlobbing 6.0.0
> Microsoft.Extensions.Http 3.1.0
> Microsoft.Extensions.Identity.Core 8.0.0
> Microsoft.Extensions.Identity.Stores 8.0.0
> Microsoft.Extensions.Localization.Abstractions 8.0.0
> Microsoft.Extensions.Logging 8.0.0
> Microsoft.Extensions.Logging.Abstractions 8.0.0
> Microsoft.Extensions.ObjectPool 7.0.0
> Microsoft.Extensions.Options 8.0.0
> Microsoft.Extensions.Primitives 8.0.0
> Microsoft.NET.StringTools 17.7.2
> Microsoft.NETCore.Platforms 1.1.0
> Microsoft.NETCore.Targets 1.1.0
> Microsoft.VisualStudio.Web.CodeGeneration 8.0.0
> Microsoft.VisualStudio.Web.CodeGeneration.Core 8.0.0
> Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore 8.0.0
> Microsoft.VisualStudio.Web.CodeGeneration.Templating 8.0.0
> Microsoft.VisualStudio.Web.CodeGeneration.Utils 8.0.0
> Microsoft.VisualStudio.Web.CodeGenerators.Mvc 8.0.0
> Microsoft.Win32.Primitives 4.3.0
> Microsoft.Win32.SystemEvents 7.0.0
> Mono.TextTemplating 2.3.1
> Newtonsoft.Json 13.0.3
> Newtonsoft.Json.Bson 1.0.2
> Npgsql 8.0.0
> NPOI 2.5.6
> NuGet.Common 6.3.1
> NuGet.Configuration 6.3.1
> NuGet.DependencyResolver.Core 6.3.1
> NuGet.Frameworks 6.3.1
> NuGet.LibraryModel 6.3.1
> NuGet.Packaging 6.3.1
> NuGet.ProjectModel 6.3.1
> NuGet.Protocol 6.3.1
> NuGet.Versioning 6.3.1
> Portable.BouncyCastle 1.8.9
> prometheus-net 8.1.0
> runtime.native.System 4.3.0
> SharpZipLib 1.3.3
> SQLitePCLRaw.bundle_e_sqlite3 2.1.6
> SQLitePCLRaw.core 2.1.6
> SQLitePCLRaw.lib.e_sqlite3 2.1.6
> SQLitePCLRaw.provider.e_sqlite3 2.1.6
> System.Buffers 4.5.1
> System.CodeDom 5.0.0
> System.Collections 4.3.0
> System.Collections.Immutable 7.0.0
> System.Composition 7.0.0
> System.Composition.AttributedModel 7.0.0
> System.Composition.Convention 7.0.0
> System.Composition.Hosting 7.0.0
> System.Composition.Runtime 7.0.0
> System.Composition.TypedParts 7.0.0
> System.Configuration.ConfigurationManager 7.0.0
> System.Data.DataSetExtensions 4.5.0
> System.Diagnostics.Debug 4.3.0
> System.Diagnostics.EventLog 7.0.0
> System.Diagnostics.Tracing 4.3.0
> System.Drawing.Common 7.0.0
> System.Formats.Asn1 5.0.0
> System.Globalization 4.3.0
> System.IO 4.3.0
> System.IO.Pipelines 7.0.0
> System.Memory 4.5.4
> System.Net.NameResolution 4.3.0
> System.Net.Primitives 4.3.0
> System.Reflection 4.3.0
> System.Reflection.Metadata 7.0.0
> System.Reflection.MetadataLoadContext 7.0.0
> System.Reflection.Primitives 4.3.0
> System.Resources.ResourceManager 4.3.0
> System.Runtime 4.3.0
> System.Runtime.CompilerServices.Unsafe 6.0.0
> System.Runtime.Extensions 4.3.0
> System.Runtime.Handles 4.3.0
> System.Runtime.InteropServices 4.3.0
> System.Security.Claims 4.3.0
> System.Security.Cryptography.Cng 5.0.0
> System.Security.Cryptography.Pkcs 5.0.0
> System.Security.Cryptography.ProtectedData 7.0.0
> System.Security.Permissions 7.0.0
> System.Security.Principal 4.3.0
> System.Security.Principal.Windows 4.3.0
> System.Text.Encoding 4.3.0
> System.Text.Encodings.Web 8.0.0
> System.Text.Json 8.0.0
> System.Threading 4.3.0
> System.Threading.Channels 7.0.0
> System.Threading.Tasks 4.3.0
> System.Threading.Tasks.Dataflow 7.0.0
> System.Windows.Extensions 7.0.0
> TestableIO.System.IO.Abstractions 19.2.87
> TestableIO.System.IO.Abstractions.Wrappers 19.2.87
@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, Key
needs to implement some more interfaces:
System.Collections.Generic.IEnumerable<byte>, System.Collections.IEnumerable, IEquatable<Key>
Here is a complete project that reproduces bug for me: efbug.tar.gz
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