Search code examples
asp.netsql-serverasp.net-mvcdbgeography

DbGeography does not work when published to a server


The following code works in my local machine while debugging but throws an error when published to a server.

public IEnumerable<Restaurant> Nearby(double latitude, double longitude, double distance)
{
    var region = DbGeography.PointFromText(string.Format("POINT({1} {0})", latitude, longitude), 4326).Buffer(10000);
    return Find(x => SqlSpatialFunctions.Filter(x.Location, region) ?? false);
}

After searching like a madman, I installed Microsoft.SqlServer.Types from nuget. (version 14.0.1016.290)

Added the required dlls in the ~/bin folder (provided by the above package).

Added the following code in Global.asax.cs

SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));

But still the code throws a "500 Internal Server Error. An error has occured".

Note: This works perfectly in my local machine.


Solution

  • Whew, finally this answer worked.

    Note: I had to include Microsoft.SqlServer.Types.dll from

    ~/packages/Microsoft.SqlServer.Types.14.0.1016.290\lib\net40 in the ~/bin folder as well