If you have a connection to SQL Server where you’re using spatial types with Entity Framework, and deploying to an Azure service/server, you may come across the exception:
Spatial types and functions are not available for this provider because the assembly 'Microsoft.SqlServer.Types' version 10 or higher could not be found.
While you can install SQL Server, a far easier way around it is to install the NuGet package Microsoft.SqlServer.Types.
After you install that NuGet package, you'll want to make a call to SqlServerTypes.Utilities.LoadNativeAssemblies prior to any actual calls in Entity Framework.
ASP.NET (e.g. in Global.asax.cs
→ Application_Start
)
SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
Standalone Application:
SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
Additionally, in the current versions of Entity Framework, there is a hardcoded reference to only look for versions 10 and 11 of the Microsoft.SqlServer.Types
assembly. You can fix this by doing the following in code (the version number should match the version you're using via the NuGet package above):
SqlProviderServices.SqlServerTypesAssemblyName = "Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91";
The final version:
ASP.NET:
SqlProviderServices.SqlServerTypesAssemblyName = "Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91";
SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
Standalone Application:
SqlProviderServices.SqlServerTypesAssemblyName = "Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91";
SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);