I am using .NET Core 3 to create a class library containing my contexts and migrations which will be shared by other applications. I can generate the initial migration just fine, but when I do the databse update I get the following error (I am using the Npgsql.EntityFrameworkCore.PostgreSQL v3.0.0-preview9 NuGet package):
Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "GENERATED"
at Npgsql.NpgsqlConnector.<>c__DisplayClass163_0.<
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass163_0.<
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable1 migrationCommands, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Exception data:
Severity: ERROR
SqlState: 42601
MessageText: syntax error at or near "GENERATED"
Position: 52
File: scan.l
Line: 1128
Routine: scanner_yyerror
42601: syntax error at or near "GENERATED"
This is the migration file that was generated:
public partial class Initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "active_threads",
columns: table => new
{
managed_thread_id = table.Column<int>(nullable: false),
name = table.Column<string>(nullable: false),
should_abort = table.Column<bool>(nullable: false),
started = table.Column<DateTimeOffset>(nullable: false),
should_refresh_printers = table.Column<bool>(nullable: false),
latest_activity = table.Column<DateTimeOffset>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_active_threads", x => new { x.managed_thread_id, x.name });
});
migrationBuilder.CreateTable(
name: "print_jobs",
columns: table => new
{
id = table.Column<int>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
print_type = table.Column<int>(nullable: false),
data_as_json = table.Column<string>(nullable: false),
printer_id = table.Column<int>(nullable: false),
requested = table.Column<DateTimeOffset>(nullable: false),
completed = table.Column<DateTimeOffset>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_print_jobs", x => x.id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "active_threads");
migrationBuilder.DropTable(
name: "print_jobs");
}
}
EF Core 3.0 switched to using the newer PostgreSQL identity columns by default, as a replacement for the older serial columns - you are probably trying to use it on a pre-10 version of PostgreSQL. This is documented in the release notes.
You can switch on compatibility, letting the provider know you are working against an older database and making it switch to serial columns instead of identity. This can be done as follows in your context's OnConfiguring method:
c#
optionsBuilder.UseNpgsql(<connection string>, o => o.SetPostgresVersion(9, 6));
I'll update the docs.
Am closing this, but can you please post back to confirm that this resolves your issue?
That was the fix. Thank you very much.
Most helpful comment
EF Core 3.0 switched to using the newer PostgreSQL identity columns by default, as a replacement for the older serial columns - you are probably trying to use it on a pre-10 version of PostgreSQL. This is documented in the release notes.
You can switch on compatibility, letting the provider know you are working against an older database and making it switch to serial columns instead of identity. This can be done as follows in your context's OnConfiguring method:
c# optionsBuilder.UseNpgsql(<connection string>, o => o.SetPostgresVersion(9, 6));I'll update the docs.