Search code examples
postgresqlasp.net-coreentity-framework-coreef-code-firstef-core-3.1

Can not update database with Entity Framework Core 3.1.3


I'm using EF Core for a ASP.Net web API application and trying to create a PostgreSQL database from my model (Basic Microsoft documentation exemple)

I manage to create the migration with "Add-Migration" but the next step, when I do "Update-Database" I have this error :

> fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
>   Failed executing DbCommand (15ms) [Parameters=[], CommandType='Text',
>   CommandTimeout='30'] CREATE TABLE "Blogs" (
>       "BlogId" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,
>       "Url" text NULL,
>       CONSTRAINT "PK_Blogs" PRIMARY KEY ("BlogId") );
> Npgsql.PostgresException (0x80004005): 42601: erreur de syntaxe sur ou près de « GENERATED »

Solution

  • Ok. So, the problem was that i wanted to use PostgreSQL 9.4, but EF Core 3 bisicly supporting only from PostgreSQL 10 and up.

    The solution is to say that you are using the version 9.4 with adding an option to the context.

    Exemple on Startup.cs :

    services.AddDbContext<MyContext>(options =>
                    options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"), o => o.SetPostgresVersion(9, 4)));
    

    Or on the context file :

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"), o => o.SetPostgresVersion(9, 4)));
            }