是否可以在实体框架和LINQ中调用T-SQL函数?

Ser*_*gey 6 c# t-sql linq entity-framework

我试过这个:

GetProvinceCodeByLatLong(a.Latitude, a.Longitude)
Run Code Online (Sandbox Code Playgroud)

我宣布这个函数是这样的:

[DbFunction("Core.Models", "fn_GetProvinceCodeByLatLong")]
public static string GetProvinceCodeByLatLong(double latitude, double longitude)
{
    throw new NotSupportedException("Direct calls are not supported.");
}
Run Code Online (Sandbox Code Playgroud)

在edmx文件中我有这个:

<Function Name="fn_GetProvinceCodeByLatLong" ReturnType="varchar" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
  <Parameter Name="latitude" Type="float" Mode="In" />
  <Parameter Name="longitude" Type="float" Mode="In" />
</Function>
Run Code Online (Sandbox Code Playgroud)

但实体框架抛出了无法构造查询的异常.

我收到此错误:

不能将类型为"Infrastructure.CustomRepositories.AssetDataRepository"的指定方法"System.String GetProvinceCodeByLatLong(Double,Double)"转换为LINQ to Entities存储表达式

Jur*_*ion -4

using (SqlConnection conn = new SqlConnection(targetConnexion))
    {
        Server server = new Server(new ServerConnection(conn));
        server.ConnectionContext.ExecuteNonQuery(File.ReadAllText(createDbScript));
    }
Run Code Online (Sandbox Code Playgroud)

其中 createDbScript 只是一个文件名。你可以放任何东西

PS:顺便说一句,它在 EF 连接内