How to configure Entity Framework to allow creation of uuid database for PostgreSQL (npgsql)?

I am updating our main project in dotnet to use PostgreSQL on the backend instead of Microsoft SQL Server and get into the situation of two tables using the GUID as the data type.

ERROR

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Property CommentID on type is a database-generated uuid, which requires the PostgreSQL uuid-ossp extension. Add .HasPostgresExtension("uuid-ossp") to your context OnModelCreating. 

After a short Google search, I realized that I had to activate the uuid extension (not sure if it could be automated somehow), and then generated the uuid in pgAdmin successfully.

 CREATE EXTENSION "uuid-ossp"; SELECT uuid_generate_v4(); 

Unfortunately, my dotnet project is still complaining about the same error. I see in error that he says adding .HasPostgresExtension ("uuid-ossp") to OnModelCreating , and I tried in several places, but I can’t figure out exactly where to add it.

+5
source share
1 answer

According to the Shay instruction - after updating to the latest version of Npgsql, the uuid extension error now disappeared. Here is a snippet of what OnModelCreating looks like for others who are trying to do this:

  protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.HasPostgresExtension("uuid-ossp"); } 
+6
source

All Articles