【问题标题】:EF Core 'The DbFunction 'ÉmpContext.fnGetEmployeeEligibility' has an invalid return type 'ÉmpMaster'' while calling table-valued functionEF Core 'The DbFunction 'ÉmpContext.fnGetEmployeeEligibility' 在调用表值函数时具有无效的返回类型 'ÉmpMaster''
【发布时间】:2020-07-14 12:41:20
【问题描述】:

我在 SQL Server fnGetEmployeeEligibility 中有一个表值函数,它接受 EmpId,并根据一些计算返回一个包含这些列的结果集:

  • EmpId (int)
  • IsEligibleForPromotion (bit)
  • IsEligibleForHike (bit)
  • IsEligibleForRelocation (bit)
  • IsEligibleForRemote (bit)
  • IsEligibleForSuperWallet (bit)

SQL 函数工作正常,我已经对其进行了测试。现在 O 需要在我的应用程序中调用这个函数,我正在使用 EF Core

这是抛出一个错误:

DbFunction“ÉmpContext.fnGetEmployeeEligibility”的返回类型“ÉmpMaster”无效。确保返回类型可以被当前提供者映射到'

Public void CheckEligiblity(int empId)
{
    var result = _empContext.fnGetEmployeeEligibility(empId);
}

在我的EmpContext 课堂上

[DBFunction("fnGetEmployeeEligibility","emp")]
Public EmpMaster fnGetEmployeeEligibility(int empId)
{
    EmpMaster empM = new EmpMaster();
    return empM;
}

我创建了一个自定义类 EmpMaster,它具有映射函数 fnGetEmployeeEligibility 的属性:

Public Class EmpMaster
{
    Public int EmpId { get; set; }
    Public bool IsEligibleForPromotion { get; set; }
    Public bool IsEligibleForHike { get; set; }
    Public bool IsEligibleForRelocation { get; set; }
    Public bool IsEligibleForRemote { get; set; }
    Public bool IsEligibleForSuperWallet { get; set; }
}

【问题讨论】:

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


【解决方案1】:

在 EF Core 5.0 中添加了对表值函数作为一等公民的支持。

表值函数可以映射到返回 IQueryable 的函数,现在可以在您的 linq 查询中完全组合。

https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#table-valued-functions

【讨论】:

  • 如何将自定义 DB 表类型直接映射到 .NET 类型,而不涉及实际表?假设我想返回任何表的查找数据(由输入参数 TABLE_NAME 定义到我的自定义 DB 函数):ID、DISPLAY_NAME、DESCRIPTION。我已经创建了 Oracle 类型并希望在 .NET 中获取 IEnumerable 查找对象列表...
猜你喜欢
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
相关资源
最近更新 更多