【问题标题】:Ef Core 7 table-valued function with nullable parameters in postgresPostgres 中具有可为空参数的 Ef Core 7 表值函数
【发布时间】:2023-02-08 03:50:49
【问题描述】:

在我的 postgres 数据库中,我有一个带有多个参数的表值函数,其中一些是可选的(默认为空)。 我想在我的 net 7 应用程序中映射此功能。 我的上下文定义看起来像这样

public IQueryable<MyEntity> GetMyEntitiesList(Guid a, string b, int c, int? d, bool? e, Guid? f)
        => FromExpression(() => GetMyEntitiesList(a, b, c, d, e, f));

和 OnModelCreating

modelBuilder.HasDbFunction(
     typeof(MyContext).GetMethod(nameof(GetMyEntitiesList)),
     new[] { typeof(Guid), typeof(string), typeof(int), typeof(int?), typeof(bool?), typeof(Guid?) }
);

当我尝试添加迁移终端输出结果时

无法搭建“System.Reflection.NullabilityInfoContext”类型的 C# 文字。提供者应实施 CoreTypeMapping.GenerateCodeLiteral 以支持在设计时使用它。

我不知道这是什么意思。

我该如何解决这个问题?我错过了什么?

【问题讨论】:

    标签: c# postgresql entity-framework-core user-defined-functions


    【解决方案1】:

    所以我不知道这是否相关,但我认为值得一提。

    我正在查看 User-defined function mapping 的 Microsoft 文档,其中有一个 HasDbFunction 看起来与您的相似。

    modelBuilder.HasDbFunction(typeof(BloggingContext).GetMethod(nameof(ActivePostCountForBlog), new[] { typeof(int) }))
        .HasName("CommentedPostCountForBlog");
    

    我创建了一个新程序来使用您的 HasDbFunction 代码测试 EFCore,但出现错误。我尝试了 Microsoft Docs 中的代码,它运行良好。

    唯一的区别是括号。

    微软

    modelBuilder.HasDbFunction(A.GetMethod(X,Y))
    

    你的

    modelBuilder.HasDbFunction(A.GetMethod(X),Y);
    

    我希望这有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 2017-06-17
      • 1970-01-01
      • 2017-06-20
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多