C# 使用实体框架6调用DB函数
我按照这些说明将标量函数添加到EntityFramework6数据模型中。 但是,我无法在LINQ查询中调用该函数,尽管直接在DataContext上调用该方法是可行的C# 使用实体框架6调用DB函数,c#,sql,linq,entity-framework-6,C#,Sql,Linq,Entity Framework 6,我按照这些说明将标量函数添加到EntityFramework6数据模型中。 但是,我无法在LINQ查询中调用该函数,尽管直接在DataContext上调用该方法是可行的 using (Entities context = new Entities()) { // This works. var Test1 = context.fn_GetRatingValue(8, 9, 0).FirstOrDefault(); // This doesn't work. va
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版本
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);
谢谢
编辑:这是我尝试使用的函数
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;
}
公共浮动?GetValue(浮动高度、浮动深度、浮动比率){
如果(高度!=null | |深度!=null){
浮动高度Calc=高度??深度值;
浮动深度计算=深度??高度值;
如果(比率<0)
深度计算=深度计算+(高度计算-深度计算)*-比率;
否则,如果(比率>0)
高度计算=高度计算+(深度计算-高度计算)*比率;
返回(浮点)数学圆(高度计算*深度计算*.12,1);
}否则
返回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);
return(float)Math.Round(
(Height.HasValue?Height.Value+(比率>0)((深度??Height.Value)-高度值)*比率:0):深度值)*
(Depth.HasValue?Depth.Value+(比率<0)((高度??Depth.Value)-深度值)*-比率:0):高度值)
* .12, 1);
我找到了答案。尽管我几乎找不到关于EntityFramework6的文档,其中EdmFunctionAttribute已经过时,但我还是让这段代码正常工作了
在EDMX文件中,IsComposable必须为True,并且必须删除CommandText。我只需要函数声明,不需要函数导入
然后,在我的数据上下文的一部分类中,我创建了这个函数
[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;
}
这管用 没有评论。有人对此有什么想法吗?我通过在EDMX文件中将所有函数参数名称都放在小写字母中,成功地删除了6046警告,但我仍然无法在查询中使用该函数。这样做会让人感到痛苦&可能是人们不想解决这个问题的原因。我要做的是在SQL中执行所有操作:从中创建一个视图。然后,您可以将视图添加为模型类(或者在Visual Studio中首先使用代码生成实体框架连接“ADO.NET Entity”时,让它为您添加视图)。然后您可以使用该模型检索结果。然后,您可以对返回的数据集执行LINQ查询,或者使用
foreach
循环提取数据,并使用将高度/深度值与比率进行比较的if
语句设置条件。我刚刚意识到,在构建LINQ查询时,DbGetValue代码甚至不会被调用!因此,您可以删除函数中的代码,或者在不调用数据库的情况下计算代码,因为只有在查询外部直接调用代码时才会调用代码。