【问题标题】:Can not update database with Entity Framework Core 3.1.3无法使用 Entity Framework Core 3.1.3 更新数据库
【发布时间】:2020-08-05 20:10:27
【问题描述】:

我将 EF Core 用于 ASP.Net Web API 应用程序并尝试从我的模型创建 PostgreSQL 数据库(基本 Microsoft 文档示例)

我设法使用“Add-Migration”创建了迁移,但是下一步,当我执行“Update-Database”时,我遇到了这个错误:

> 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 »

【问题讨论】:

    标签: postgresql asp.net-core entity-framework-core ef-code-first ef-core-3.1


    【解决方案1】:

    好的。所以,问题是我想使用 PostgreSQL 9.4,但 EF Core 3 基本上只支持 PostgreSQL 10 及更高版本。

    解决方案是说您使用的是 9.4 版本,并在上下文中添加了一个选项。

    Startup.cs 示例:

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

    或者在上下文文件中:

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

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 2020-02-13
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2017-08-01
      • 2016-05-16
      • 1970-01-01
      相关资源
      最近更新 更多