【问题标题】:Model defined function not recognized模型定义的功能无法识别
【发布时间】:2015-06-04 21:42:06
【问题描述】:

我有一个带有 .edmx 文件的 ASP.NET 4.0 MVC 项目,使用 Linq to Entities。我在概念模型中添加了一个函数,但出现“不支持”错误。

EF 版本:

<package id="EntityFramework" version="6.1.3" targetFramework="net40" />

这里是 edmx 部分:

<edmx:ConceptualModels>
  <Schema Namespace="MillCertsModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2009/11/edm" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
  ... EntityType elements ...
  <Function Name="IsLike" ReturnType="Edm.Boolean">
      <Parameter Name="str" Type="Edm.String" />
      <Parameter Name="pattern" Type="Edm.String" />
      <DefiningExpression>
        str LIKE pattern
      </DefiningExpression>
    </Function>
  </Schema>
</edmx:ConceptualModels>

还有函数...

public static class Utilities
{
    [System.Data.Objects.DataClasses.EdmFunction("MillCertsModel", "IsLike")]
    public static bool IsLike(this string str, string pattern)
    {
        throw new NotSupportedException("Supported for SQL queries only.");
    }
}

还有代码...

if (keywords != null)
   query = query.Where(m => keywords.Any(kw => m.CertificateData.Any(cd => cd.value.ToLower() == kw || cd.value.IsLike("[^a-z]"))));

错误:LINQ to Entities 无法识别方法 'Boolean IsLike(System.String, System.String)' 方法,并且此方法无法转换为存储表达式。

【问题讨论】:

    标签: c# asp.net asp.net-mvc linq linq-to-entities


    【解决方案1】:

    发现问题。命名空间错误。

    正确的命名空间:

    System.Data.Entity.Core.Objects.DataClasses
    
    // NOT: System.Data.Objects.DataClasses.EdmFunction
    

    但是,此属性现在已过时。建议改为System.Data.Entity.DbFunctionAttribute

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多