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

c# – 使用Entity Framework 6调用DB函数,第1张

概述我按照这些说明将标量函数添加到我的Entity Framework 6数据模型中. How to use scalar-valued function with linq to entity? 但是,我无法在LINQ查询中调用该函数,尽管直接在DataContext上调用该方法也可以. using (Entities context = new Entities()) { // This 我按照这些说明将标量函数添加到我的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();

这有效!

总结

以上是内存溢出为你收集整理的c# – 使用Entity Framework 6调用DB函数全部内容,希望文章能够帮你解决c# – 使用Entity Framework 6调用DB函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1249864.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存