【问题标题】:How do I configure Entity Framework to allow database-generate uuid for PostgreSQL (npgsql)?如何配置实体框架以允许数据库为 PostgreSQL (npgsql) 生成 uuid?
【发布时间】:2016-10-01 09:27:19
【问题描述】:

我正在更新我们的 dotnet 核心项目以在后端使用 PostgreSQL 而不是 Microsoft SQL Server,并且遇到了两个使用 GUID 作为数据类型的表的情况。

错误

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's OnModelCreating.

经过一番谷歌搜索后,我意识到我必须启用 uuid 扩展(不确定这是否可以以某种方式自动化),然后在 pgAdmin 中成功生成了一个 uuid。

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

不幸的是,我的 dotnet 项目仍然在抱怨同样的错误。我在错误中看到 add .HasPostgresExtension("uuid-ossp") to OnModelCreating 并且我已经尝试了几个地方,但似乎无法弄清楚应该在哪里添加它。

【问题讨论】:

  • HasPostgresExtension() 需要放在上下文的 OnModelCreating 中,如错误所示。如果不是,请发布您的上下文的相关部分。请注意,您必须至少使用 Npgsql 不稳定提要中的 1.0.0-rc3-ci-39 版本才能使其正常工作。

标签: asp.net postgresql asp.net-core entity-framework-core npgsql


【解决方案1】:

按照 Shay 的指示 - 更新到最新版本的 Npgsql 后,uuid 扩展错误现在消失了。这是其他人尝试使用的 OnModelCreating 的外观:

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.HasPostgresExtension("uuid-ossp");
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2016-05-13
    • 1970-01-01
    • 2018-12-20
    • 2013-03-26
    • 1970-01-01
    • 2011-05-15
    相关资源
    最近更新 更多