【问题标题】:Is it possible to call T-SQL function in Entity Framework and LINQ?是否可以在实体框架和 LINQ 中调用 T-SQL 函数?
【发布时间】:2014-02-21 22:58:14
【问题描述】:

我试过这个:

GetProvinceCodeByLatLong(a.Latitude, a.Longitude)

我这样声明函数:

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

在 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>

但是Entity Framework抛出无法构造查询的异常。

我收到此错误:

“Infrastructure.CustomRepositories.AssetDataRepository”类型上的指定方法“System.String GetProvinceCodeByLatLong(Double, Double)”无法转换为 LINQ to Entities 存储表达式

【问题讨论】:

标签: c# linq entity-framework tsql


【解决方案1】:
using (SqlConnection conn = new SqlConnection(targetConnexion))
    {
        Server server = new Server(new ServerConnection(conn));
        server.ConnectionContext.ExecuteNonQuery(File.ReadAllText(createDbScript));
    }

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

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

【讨论】:

  • 对不起,这不是这个问题
猜你喜欢
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
  • 1970-01-01
相关资源
最近更新 更多