Sql server 如何在EF中从数据库调用函数

Sql server 如何在EF中从数据库调用函数,sql-server,entity-framework,entity-framework-4.1,Sql Server,Entity Framework,Entity Framework 4.1,我从数据库生成一个模型 在.edmx文件中,我有一个行字符串 <Function Name="GetUniqueInt" ReturnType="int" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" /> 原因是什么?您需要在某个地方创建。它应该

我从数据库生成一个模型

在.edmx文件中,我有一个行字符串

<Function Name="GetUniqueInt" ReturnType="int" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" />

原因是什么?

您需要在某个地方创建。它应该是这样的:

[EdmFunction("YourModelNamespace", "GetUniqueInt")]
public static int GetUniqueInt()
{
    throw new NotSupportedException("Direct calls are not supported.");
}

例如,将此方法放在您的上下文类中,并在LINQ查询中使用它。

我不理解“抛出新的NotSupportedException(“不支持直接调用”);“不必更改?这是存根方法。”。它应该只在LINQtoEntities查询中调用,因为它的内容被转换为SQL函数调用,所以永远不会被调用。