This post was most recently updated on September 30th, 2022.
2 min read.This page describes multiple ways to fix the ‘Spatial types and functions are not available for this provider because the assembly ‘Microsoft.SqlServer.Types’ version 10 or higher could not be found.‘ exception which comes up during debugging or publishing your program, app or service using DbGeography.
Symptoms
While running a console program or perhaps installing an ASP.NET MVC website on a machine, where SQL Server (apart from the one that comes with the Visual Studio) has not been installed, you may encounter the following error:
Spatial types and functions are not available for this provider because the assembly ‘Microsoft.SqlServer.Types’ version 10 or higher could not be found.
At least for me, this was baffling as that assembly was included in the project, and would compile and run flawlessly on other machines, just not on this one. I’m documenting here all the possible fixes to the issue I am aware of.
Problem / Reason
For me, the actual reason was that even though the DLL was loaded and included in the project as a nuget package, the actual native assemblies for this dll were not loaded to memory, as this needs to be done in the runtime. This issue was made worse by the fact that I first created this solution on a machine, where SQL Server was installed, so I was very much oblivious of the issue until it hit me on the face while trying to run my program on this particular machine, which did not have SQL Server.
Solution
After googling around, I found a few non-working solutions – they may work in other contexts, so I’m documenting them here.
Time needed: 5 minutes
Try the following to fix the ‘Spatial types and functions are not available for this provider because the assembly ‘Microsoft.SqlServer.Types’ version 10 or higher could not be found.‘ error
- Install the Microsoft.SqlServer.Types NuGet package.
Install-Package Microsoft.SqlServer.Types
For my other dev machine, this alone was enough to get me going.
- If you have it already, and the issue persists, reinstall it:
Update-Package -reinstall Microsoft.SqlServer.Types
For me, this added the much-needed dlls to the actual project, as they didn’t exist on this machine (but did on the other, where I first ran this command).
If it’s still throwing the exception, you’ll need to load the assemblies on the fly - ASP.NET applications:
For ASP.NET applications, add the following line of code to the Application_Start method in Global.asax.cs:
SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
- Desktop applications
For desktop applications, add the following line of code to run before any spatial operations are performed:
qlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
For the record, this worked for me.
If it’s still not working, you can try installing the SQL server on the dev machine. Pain in the ass, I know, but it should fix the situation in case nothing else works.
Good luck!
- “Performing cleanup” – Excel is stuck with an old, conflicted file and will never recover. - November 12, 2024
- How to add multiple app URIs for your Entra app registration? - November 5, 2024
- How to access Environment Secrets with GitHub Actions? - October 29, 2024