我想将内置的 SQL 2012 FORMAT 函数添加到静态函数类以与 LINQ to Entity Framework 一起使用。
我关注了this模拟添加内置 SQL 函数的源代码。
public static class ExtendSqlFunctions
{
[DbFunction("SqlServer", "FORMAT")]
public static string Format(Int64? arg, string formatType, string culture)
{
throw new NotSupportedException();
}
}
ERROR: The specified method 'System.String Format(System.Nullable`1[System.Int64], System.String, System.String)' on the type 'ExtendSqlFunctions' cannot be translated into a LINQ to Entities store expression.
更新
通过将“FORMAT”添加到 Entity Framework 模型的 SSDL,我能够获得一个工作版本。
所以看起来 Microsoft 不支持其内部库之外的“DbFunction SqlServer”。
对此不确定?
http://msdn.microsoft.com/en-us/library/vstudio/bb738614(v=vs.100).aspx
如果有人知道如何在不编辑 SSDL 的情况下添加内置 SQL 函数,请告诉我。
更新
我已将此添加为 CodePlex 上的一个问题
https://entityframework.codeplex.com/workitem/2586
并且“rjperes”将其添加为 CodePlex 上的讨论。
https://entityframework.codeplex.com/discussions/572518
最佳答案
如果您使用代码优先(从您的问题中不清楚),除了应用 DbFunction 属性外,您还需要在模型上的 OnModelCreating 方法中注册函数。您在 DbFunction 属性中设置的名称和命名空间必须与对 EdmFunction.Create 的调用相匹配。在此处查看完整示例:Registering SQL Server Built-in Functions to Entity Framework Code First
关于c# - 将 SQL 2012 FORMAT 函数与 LINQ to Entity Framework 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019790/