npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.54k stars 226 forks source link

Error compiling optimized DbContext with concurrency token on 8.0 #2992

Closed isaac-ferrer-tmt closed 10 months ago

isaac-ferrer-tmt commented 11 months ago

I have some entities with a uint concurrency token, mapped to the xmin system column, for example:

    public class SomeEntity
    {
        public int Id { get; set; }

        [Timestamp]
        public uint Version { get; set; }
    }

I have upgraded to 8.0. When I run Optimize-DbContext to compile the context, I get the following exception:

System.InvalidOperationException: The type mapping used is incompatible with a compiled model. The mapping type must have a 'public static readonly NpgsqlUIntTypeMapping NpgsqlUIntTypeMapping.Default' property.
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpRuntimeAnnotationCodeGenerator.CreateDefaultTypeMapping(CoreTypeMapping typeMapping, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
   at Microsoft.EntityFrameworkCore.Design.Internal.RelationalCSharpRuntimeAnnotationCodeGenerator.Create(CoreTypeMapping typeMapping, CSharpRuntimeAnnotationCodeGeneratorParameters parameters, ValueComparer valueComparer, ValueComparer keyValueComparer, ValueComparer providerValueComparer)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Design.Internal.NpgsqlCSharpRuntimeAnnotationCodeGenerator.Create(CoreTypeMapping typeMapping, CSharpRuntimeAnnotationCodeGeneratorParameters parameters, ValueComparer valueComparer, ValueComparer keyValueComparer, ValueComparer providerValueComparer)
   at Microsoft.EntityFrameworkCore.Design.Internal.ICSharpRuntimeAnnotationCodeGenerator.Create(CoreTypeMapping typeMapping, IProperty property, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.Create(IProperty property, String variableName, Dictionary`2 propertyVariables, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.Create(IProperty property, Dictionary`2 propertyVariables, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.CreateEntityType(IEntityType entityType, IndentedStringBuilder mainBuilder, IndentedStringBuilder methodBuilder, SortedSet`1 namespaces, String className, Boolean nullable)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.GenerateEntityType(IEntityType entityType, String namespace, String className, Boolean nullable)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.GenerateModel(IModel model, CompiledModelCodeGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CompiledModelScaffolder.ScaffoldModel(IModel model, String outputDir, CompiledModelCodeGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.Optimize(String outputDir, String modelNamespace, String contextTypeName)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OptimizeContextImpl(String outputDir, String modelNamespace, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OptimizeContext.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

It was working fine on 7.0.

roji commented 11 months ago

Thanks, I'll look at this and fix it fox 8.0.1.

springy76 commented 11 months ago

Same problem here but I just don't have any uint anywhere in my models, only int32 and int64.

Of course there was an unseen uint in a mapped record:

public record DatabaseInfoEntity(
    [property: Key, Column("oid", TypeName = "oid")] uint ObjectId,
    [property: Column("datname")] string DbName,
    [property: Column("description")] string? Description)

Removed this one and now a compiled model gets created - but it doesn't compile due to private nested classes being referenced: https://github.com/npgsql/efcore.pg/issues/2972 (and I can't change the class as it comes from IdentityFramework: property DateTimeOffSet? LockoutEnd in user entity)