Can I call a T-SQL function in Entity Framework and LINQ?

I tried the following:

GetProvinceCodeByLatLong(a.Latitude, a.Longitude) 

And I declared this function as follows:

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

In the edmx file, I have the following:

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

But the Entity Framework throws an exception that cannot be thrown.

This error I get:

The specified method "System.String GetProvinceCodeByLatLong (Double, Double)" in the type "Infrastructure .CustomRepositories.AssetDataRepository" cannot be translated into a storage expression LINQ to Entities

+6
source share
1 answer
 using (SqlConnection conn = new SqlConnection(targetConnexion)) { Server server = new Server(new ServerConnection(conn)); server.ConnectionContext.ExecuteNonQuery(File.ReadAllText(createDbScript)); } 

where createDbScript is just the file name. Can you put something

PS: It is inside the EF btw connector

-4
source

All Articles