I followed these instructions to add a scalar function into my Entity Framework 6 data model. How to use scalar-valued function with linq to entity?
However, I\'m no
I found the answer. Although I could find very little documentation about Entity Framework 6 in which EdmFunctionAttribute is obsolete, I got this code to work.
In the EDMX file, IsComposable must be True and the CommandText must be removed. I need only the function declaration without the function import.
Then, in a partial class of my data context, I created this function
[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;
}
I added the function to the MediaRating object so I can call it without needing a reference to the data context.
var Test2 = (from r in context.MediaRatings
select r.DbGetValue(r.Height, r.Depth, 0)).ToList();
This works!