实体框架标量函数映射 [英] Entity Framework scalar function mapping

查看:174
本文介绍了实体框架标量函数映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 创建功能[dbo]。 @locationId Int 

RETURNS位
AS
BEGIN
//代码
END

我想在Entity Framework上下文中使用它。



我已经在* .edmx文件中添加了:

 < Function Name =CheckLocationReturnType =bitAggregate =falseBuiltIn =falseNiladicFunction = falseIsComposable =trueParameterTypeSemantics =AllowImplicitConversionSchema =dbo> 
< Parameter Name =locationIdType =intMode =In/>
< / Function>

我还创建了一个使用EdmFunctionAttribute装饰的方法的部分类:

  public partial class MainModelContainer 
{
[EdmFunction(MainModel.Store,CheckLocation)]
public bool CheckLocation (int locationId)
{
throw new NotSupportedException(Direct calls not supported);
}
}

我尝试使用这样的功能: p>

  Context.CheckLocation(locationId); 

并获取NotSupportedException(直接调用不支持)。
它可以在Select方法中运行,但它不适合我。
请帮忙!
如何在没有选择方法的情况下调用此函数?

解决方案

您需要将其作为选择访问

  var students = context.Locations 
.Select(new {location = CheckLocation(locationId)}):


I have scalar function:

CREATE FUNCTION [dbo].[CheckLocation]
(
    @locationId Int
)
RETURNS bit
AS
BEGIN
    //code
END

I want to use it in Entity Framework context.

I have added this in the *.edmx file:

<Function Name="CheckLocation" ReturnType="bit" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" >
<Parameter Name="locationId" Type="int" Mode="In" />
</Function>

I have also created a partial class with method decorated with EdmFunctionAttribute:

public partial class MainModelContainer
{
    [EdmFunction("MainModel.Store", "CheckLocation")]
    public bool CheckLocation(int locationId)
    {
        throw new NotSupportedException("Direct calls not supported");
    }
}

I try to use this function like this:

Context.CheckLocation(locationId);

And get NotSupportedException("Direct calls not supported"). It works within Select method, but it does not suit me. Help please! How can I call this function without select method?

解决方案

you need to access it as a select

var students = context.Locations
    .Select ( new  {  location= CheckLocation(locationId)}):

这篇关于实体框架标量函数映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆