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
{{ message }}
Repository navigation
Types not being reloaded properly after adding extension and running Migrate() #292
Hi, I have installed the newest Npgsql.EntityFrameworkCore.PostgreSQL package in my project, and I added the 'hstore' extension like this: modelBuilder.HasPostgresExtension("hstore");
The migration file content generated like this: migrationBuilder.AlterDatabase().Annotation("Npgsql:PostgresExtension:hstore", "'hstore', '', ''");
After this, I have my own database initialize process in main function:
I invoke the Migrate() function in order to update my database automatically, after this, I checked that the extension 'hstore' already added to my database, but when I insert records to table(which contains a field with data type Dictionary<string, string>) I got this exception: "NpgsqlException: The PostgreSQL type 'hstore', mapped to NpgsqlDbType 'Hstore' isn't present in your database. You may need to install an extension or upgrade to a newer version."
I'm not sure how to resolve this issue, can anybody help me?
Thanks.
In addition, if I do not invoke dbContext.Database.Migrate() function explicitly, just use common line: "dotnet ef database update" to update my database, everything is ok when inserting records to database.
This is probably because after adding the extension in the database, Npgsql (the ADO layer) needs to reload the types to discover the newly-added hstore... This was supposed to be taken care of, but there may be an issue - I'll try to take a look soon.
The workaround is indeed to run the migration from a different process - if you simply restart your application everything should work fine.
I create database in integration test, I use context.Database.Migrate() but it fails with same error. When I added ReloadTypes after database migration and it started working
using (var ctx = server.Host.Services.GetService<MyContext>())
using (var conn = new NpgsqlConnection(ctx.Database.GetDbConnection().ConnectionString))
{
conn.Open();
conn.ReloadTypes();
conn.Close();
}
Pulled 2.0.2-166 and unfortunetelty still not works, but an error is different, without explicit ReloadTypes I have:
System.NotSupportedException: The field 'Code' has a type currently unknown to Npgsql (OID 22894). You can retrieve it as a string by marking it as unknown, please see the FAQ
@roji I use citext extension: modelBuilder.HasPostgresExtension("citext");
Code has type citext builder.Property(x => x.Code).HasColumnType("citext");
Give me some time, I will try to reproduce it later.
changed the title [-]Types not being reloaded properly after adding extension in migrations[/-][+]Types not being reloaded properly after adding extension and running Migrate()[/+]on Nov 16, 2023
@OlegTopalov no, that's not planned. Note that it's very easy to work around this - just add ReloadTypes() after calling Migrate() in your code (see above).
I was getting a "Can't cast database type . to Point" error when I try to get a geometry data from pgsql database with using entity framework in my .net 7 web api and also getting "The NpgsqlDbType 'Geometry' isn't present in your database. You may need to install an extension or upgrade to a newer version." error when I try to insert a new geometry data (point in this case) pgsql database. I searched for a solution, tried almost everything but nothing worked and then I found this answer (#292 (comment)) in this thread and applied it and it worked! Here is my code.
Just in case some runs across this, the new data seeding mechanism introduced in EF Core 9 (docs) exposes the same issue mentioned here.
Same workaround works though. In your seeding code just do something like shown below.
For me this popped up with a custom enum type, mapped through MapEnum<T>(). Which works fine without the workaround if you skip the seeding call.
awaitcontext.Database.OpenConnectionAsync(cancellationToken);await((NpgsqlConnection)context.Database.GetDbConnection()).ReloadTypesAsync(cancellationToken);awaitcontext.Database.CloseConnectionAsync();// actual seeding follows
Hope this helps.
Reacted by neildavisonwae, Daniel Gioulakis and Peter
Activity
kusey commentedon Feb 2, 2018
In addition, if I do not invoke dbContext.Database.Migrate() function explicitly, just use common line: "dotnet ef database update" to update my database, everything is ok when inserting records to database.
roji commentedon Feb 2, 2018
This is probably because after adding the extension in the database, Npgsql (the ADO layer) needs to reload the types to discover the newly-added hstore... This was supposed to be taken care of, but there may be an issue - I'll try to take a look soon.
The workaround is indeed to run the migration from a different process - if you simply restart your application everything should work fine.
[-]issue about add extension 'hstore'[/-][+]Types not being reloaded properly after adding extension in migrations[/+]kusey commentedon Feb 2, 2018
Thanks for your reply.
If it's a bug I will find some workarounds temporary.
myshon commentedon Feb 14, 2018
I create database in integration test, I use context.Database.Migrate() but it fails with same error. When I added ReloadTypes after database migration and it started working
roji commentedon Feb 27, 2018
Oops, I ran into this today and fixed it as #313, forgetting that this issue was open.
Can you please test version 2.0.2-166 from the stable feed and confirm that the problem goes away?
myshon commentedon Feb 28, 2018
Pulled 2.0.2-166 and unfortunetelty still not works, but an error is different, without explicit
ReloadTypesI have:roji commentedon Feb 28, 2018
@myshon what's the type of
Code? Any chance you could post a small code sample reproducing the issue? Things seem to work well on my end.myshon commentedon Feb 28, 2018
@roji I use
citextextension:modelBuilder.HasPostgresExtension("citext");Code has type
citextbuilder.Property(x => x.Code).HasColumnType("citext");Give me some time, I will try to reproduce it later.
roji commentedon Feb 28, 2018
Great, thanks - a simple small context with some minimal code would be ideal.
kusey commentedon Mar 1, 2018
@roji I still got the same error using pkg 2.0.2-ci-00166.
roji commentedon Mar 1, 2018
@kusey as I wrote above, a small code sample would be very helpful here. In my scenario it's OK, and I don't know what you're doing differently.
kusey commentedon Mar 5, 2018
@roji Here is my code, executing pending migrations when website startup:
and the database initialize code like this:
Sorry for the late reply, i'm going crazy that github always show error message 'You can't comment at this time'.
34 remaining items
[-]Types not being reloaded properly after adding extension in migrations[/-][+]Types not being reloaded properly after adding extension and running Migrate()[/+]Call Npgsql's ReloadTypes as needed from programmatic Migrate()
Call Npgsql's ReloadTypes as needed from programmatic Migrate() (#2951)
OlegTopalov commentedon Nov 28, 2023
@roji Will this fix be added to 6.x versions? We are currently not planning to update to 8.x versions
roji commentedon Nov 28, 2023
@OlegTopalov no, that's not planned. Note that it's very easy to work around this - just add ReloadTypes() after calling Migrate() in your code (see above).
umutdr commentedon Nov 19, 2024
I was getting a "Can't cast database type . to Point" error when I try to get a geometry data from pgsql database with using entity framework in my .net 7 web api and also getting "The NpgsqlDbType 'Geometry' isn't present in your database. You may need to install an extension or upgrade to a newer version." error when I try to insert a new geometry data (point in this case) pgsql database. I searched for a solution, tried almost everything but nothing worked and then I found this answer (#292 (comment)) in this thread and applied it and it worked! Here is my code.
mlichtner commentedon Nov 24, 2024
Just in case some runs across this, the new data seeding mechanism introduced in EF Core 9 (docs) exposes the same issue mentioned here.
Same workaround works though. In your seeding code just do something like shown below.
For me this popped up with a custom enum type, mapped through
MapEnum<T>(). Which works fine without the workaround if you skip the seeding call.Hope this helps.