c# – 使用Entity Framework 6调用DB函数

我按照这些说明将标量函数添加到我的Entity Framework 6数据模型中.
How to use scalar-valued function with linq to entity?

但是,我无法在LINQ查询调用函数,尽管直接在DataContext上调用方法也可以.

using (Entities context = new Entities()) {
    // This works.
    var Test1 = context.fn_GetratingValue(8,9,0).FirstOrDefault();
    // This doesn't work.
    var Test2 = (from r in context.Mediaratings
                select context.fn_GetratingValue(r.Height,r.Depth,0)).ToList();
}

第二个查询抛出此错误.

LINQ to Entities does not recognize the method 'System.Data.Entity.Core.Objects.ObjectResult`1[System.Nullable`1[System.Single]] fn_GetratingValue(System.Nullable`1[System.Single],System.Nullable`1[System.Single],System.Nullable`1[System.Single])' method,and this method cannot be translated into a store expression.

此外,设计师正在给我这个警告

Error 6046: Unable to generate function import return type of the store function 'fn_GetratingValue'. The store function will be ignored and the function import will not be generated.

我究竟做错了什么?如何在LINQ查询调用数据库函数

此外,如果查询代码有时会针对数据库执行,有时会在内存中执行,那么有没有办法以两种情况下的方式调用函数我有一个相同功能的C#版本.

谢谢

编辑:这是我正在尝试使用的功能.

public float? GetValue(float? Height,float? Depth,float ratio) {
    if (Height != null || Depth != null) {
        float HeightCalc = Height ?? Depth.Value;
        float DepthCalc = Depth ?? Height.Value;
        if (ratio < 0)
            DepthCalc = DepthCalc + (HeightCalc - DepthCalc) * -ratio;
        else if (ratio > 0)
            HeightCalc = HeightCalc + (DepthCalc - HeightCalc) * ratio;
        return (float)Math.Round(HeightCalc * DepthCalc * .12,1);
    } else
        return null;
}

它也可以像这样写成一行.这条线可以在我需要使用它的任何地方进行复制/粘贴,但这会产生非常难看的代码,尽管这可能有效.我宁愿把它作为一个功能.

return (float)Math.Round(
    (Height.HasValue ? Height.Value + (ratio > 0 ? ((Depth ?? Height.Value) - Height.Value) * ratio : 0) : Depth.Value) *
    (Depth.HasValue ? Depth.Value + (ratio < 0 ? ((Height ?? Depth.Value) - Depth.Value) * -ratio : 0) : Height.Value)
    * .12,1);

解决方法

我找到了答案.虽然我发现很少有关于EdmFunctionAttribute已经过时的Entity Framework 6的文档,但我得到了这段代码.

在EDMX文件中,IsComposable必须为True,并且必须删除CommandText.我只需要没有函数import的函数声明.

然后,在我的数据上下文的部分类中,我创建了这个函数

[DbFunction("NaturalGroundingVideosModel.Store","fn_GetratingValue")]
public float? DbGetValue(float? height,float? depth,float ratio) {
    List<ObjectParameter> parameters = new List<ObjectParameter>(3);
    parameters.Add(new ObjectParameter("height",height));
    parameters.Add(new ObjectParameter("depth",depth));
    parameters.Add(new ObjectParameter("ratio",ratio));
    var lObjectContext = ((IObjectContextAdapter)this).ObjectContext;
    var output = lObjectContext.
            createquery<float?>("NaturalGroundingVideosModel.Store.fn_GetratingValue(@height,@depth,@ratio)",parameters.ToArray())
        .Execute(MergeOption.NoTracking)
        .FirstOrDefault();
    return output;
}

我将该函数添加到Mediarating对象,因此我可以调用它而无需引用数据上下文.

var Test2 = (from r in context.Mediaratings
    select r.DbGetValue(r.Height,0)).ToList();

这有效!

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...