This page describes mutiple ways on how to fix 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.‘ which comes up during debugging or publishing your program, app or service using DbGeography.
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.
After googling around, I found a few non-working solutions – they may work in other contexts, so I’m documenting them here.
- Install the Microsoft.SqlServer.Types NuGet package.
For my another dev machine this alone was enough to get me going.
- If you have it already, and the issue persist, 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 – this can be done this way:
For ASP.NET applications, add the following line of code to the Application_Start method in Global.asax.cs:
For desktop applications, add the following line of code to run before any spatial operations are performed:
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.
Latest posts by Antti K. Koskela (see all)
- How to fix an Office Group with no Owners - November 14, 2018
- Help! My “Content and Structure” page in SharePoint Online is gone! - November 7, 2018
- Thanks for coming to my session at SPS New England 10/20 ! - October 20, 2018
- Speaking at SPS New England on 10/20! - October 17, 2018