微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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();

这有效!

原文地址:https://www.jb51.cc/csharp/243820.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐