c# - 将 SQL 2012 FORMAT 函数与 LINQ to Entity Framework 结合使用

标签 c# linq entity-framework

我想将内置的 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/

相关文章:

c# - WPF 的 XAML : Hide ToolTip Popup when blank Tooltip text

c# - 将相同的对象保存在不同的表中

c# - 删除特定列 RadGrid MVVM 的格式

C# 我可以将命令提示符控件添加到我的应用程序吗

.net - 是否可以使 LinQ "understand"成为经典的 SQL 查询?

c# - 将 LINQ to EF 查询加速到最大

C# 将一个列表与另一个列表的一部分进行比较

c# - 加载虚拟属性

c# - 从多对多关系中仅删除一个条目

c# - 如何从服务器中的客户端池中识别客户端 - 设计