无法识别模型定义的函数

Model defined function not recognized

我有一个带有 .edmx 文件的 ASP.NET 4.0 MVC 项目,使用 Linq to Entities。我向概念模型添加了一个函数,但出现 "not supported" 错误。

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)' 方法,并且无法将此方法转换为存储表达式。

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

正确的命名空间:

System.Data.Entity.Core.Objects.DataClasses

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

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