Home > Software engineering >  Npgsql.PostgresException: type "ltree" does not exist
Npgsql.PostgresException: type "ltree" does not exist

Time:02-16

I'm developing a WebAPI with asp.net core [net5.0]

  • DataBase: PostgreSQL
  • Provider: EF Core
  • Approach: Code First

In domain objects I've used Ltree for hierarchy. While adding and updating migration, getting followed error: Npgsql.PostgresException: type "ltree" does not exist.

I did some research and found that, If I run "create extension Ltree" on PostgreSQL database, this will create extension for Ltree.

In code first approach database will be created while adding and updating migrations, so I can not go to database and create an extension for that.

My question is: is that possible creating an extension in migration?

public class Department
{
   public int Id { get; set; }
   
   [Column(TypeName = "Ltree")]
   public string Ltree { get; set; }
   
   public string Name { get; set; }
}

CodePudding user response:

You can have EF Core create the extension for you by adding the following line in your model-building code (e.g. OnModelCreating):

modelBuilder.HasPostgresExtension("ltree");

Note that in version 7.0, the provider will automatically detect that an extension is needed (by going over your model properties), and will add the appropriate PostgreSQL extension without you having to do that explicitly (see https://github.com/npgsql/efcore.pg/issues/2137).

  • Related