You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
When upgrading to .NET 9, I have this error unsafe use of new value "entity3" of enum type entity_type when adding a new value to an existing enum in a first migration, using this value in a second migration and running both of these migrations at the same time. This error occurs when using await dbContext.Database.MigrateAsync(); or dotnet ef database update.
Before upgrading (in .NET 8) I had this error only when I was using the new added value in the same migration as its addition.
Here's a minimal code and steps to replicate the error :
Create an initial migration with Entity3 still commented in EntityType enum
Uncomment Entity3 and create another migration
Create a third empty migration and add this code in the Up() : migrationBuilder.Sql("INSERT INTO entity (id, type) VALUES (uuid_generate_v4(), 'entity3')");
Run the app or use dotnet ef database update
usingMicrosoft.EntityFrameworkCore;usingNpgsql;varbuilder=WebApplication.CreateBuilder(args);NpgsqlDataSourceBuilderdataSourceBuilder=new("Server=localhost; Port=5432; Database=sandbox; Userid=postgres; Password=admin;");dataSourceBuilder.MapEnum<EntityType>();vardataSource=dataSourceBuilder.Build();// .NET 9builder.Services.AddDbContext<AppDbContext>(options =>{options.UseNpgsql(dataSource, opts =>opts.MapEnum<EntityType>());});// .NET 8//builder.Services.AddDbContext<AppDbContext>(options => { options.UseNpgsql(dataSource); });varapp=builder.Build();awaitusingvarscope=app.Services.CreateAsyncScope();awaitusingvardbContext=scope.ServiceProvider.GetRequiredService<AppDbContext>();awaitdbContext.Database.MigrateAsync();awaitapp.RunAsync();publicclassAppDbContext:DbContext{publicAppDbContext(){}publicAppDbContext(DbContextOptions<AppDbContext>options):base(options){}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.HasPostgresExtension("uuid-ossp");// .NET 8//modelBuilder.HasPostgresEnum<EntityType>();varentityBuilder=modelBuilder.Entity<Entity>();entityBuilder.ToTable("entity");entityBuilder.HasKey(x =>x.Id);entityBuilder.Property(x =>x.Id).HasColumnName("id");entityBuilder.Property(x =>x.Type).HasColumnName("type");}}publicclassEntity{publicGuidId{get;set;}publicEntityTypeType{get;set;}}publicenumEntityType{Entity1,Entity2// Uncomment before adding the second migration//Entity3}
There is no error when updating the database first to the second migration and then to the third so to me it seems like a missing commit; or something like that between migrations in the same run.
And I'm not sure that this is relevant to this issue but when using await dbContext.Database.MigrateAsync(); to create the database at the first run of the app (which I know is not a good practice but I need to use it in my integration tests) there is this error An error occurred using the connection to database 'sandbox' on server 'tcp://localhost:5432'. but it's still create and migrate the database. You can see this error in the console when running the app with the above steps. And this error was not present in the previous versions.
The text was updated successfully, but these errors were encountered:
Yeah, this is the result of an EF9 change, where all migrations are now executed within a single transaction, and PostgreSQL doesn't support adding an enum value and using it in the same transaction; this has been flagged here: dotnet/efcore#35096 (comment). We'll be discussing what to do on this kind of issue on the EF side, in the meantime you can suppress the transaction, or simply apply the migrations in two steps (first go up to the migration that adds the enum, then the rest).
Hello,
When upgrading to .NET 9, I have this error
unsafe use of new value "entity3" of enum type entity_type
when adding a new value to an existing enum in a first migration, using this value in a second migration and running both of these migrations at the same time. This error occurs when usingawait dbContext.Database.MigrateAsync();
ordotnet ef database update
.Before upgrading (in .NET 8) I had this error only when I was using the new added value in the same migration as its addition.
Here's a minimal code and steps to replicate the error :
Entity3
still commented inEntityType
enumEntity3
and create another migrationmigrationBuilder.Sql("INSERT INTO entity (id, type) VALUES (uuid_generate_v4(), 'entity3')");
dotnet ef database update
There is no error when updating the database first to the second migration and then to the third so to me it seems like a missing
commit;
or something like that between migrations in the same run.And I'm not sure that this is relevant to this issue but when using
await dbContext.Database.MigrateAsync();
to create the database at the first run of the app (which I know is not a good practice but I need to use it in my integration tests) there is this errorAn error occurred using the connection to database 'sandbox' on server 'tcp://localhost:5432'.
but it's still create and migrate the database. You can see this error in the console when running the app with the above steps. And this error was not present in the previous versions.The text was updated successfully, but these errors were encountered: