In code-first database, if an entity class contains the DbGeography type, add-migration fails with 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."
Interestingly, even if the code is generated from an existing database that uses that data type, adding migrations after the fact will still fail.
Steps to reproduce:
I have tried:
@Colorfulmoose Getting the spatial types installed can be tricky. I would try downloading and installing the appropriate feature--for example, https://www.microsoft.com/en-us/download/details.aspx?id=52676.
@Colorfulmoose Recently also added this to an existing project.
Microsoft.SqlServer.Types version 11.0.2 as while the error reports 10 or higher, it's really 10 or 11. See Issue https://github.com/aspnet/EntityFramework6/issues/244 - Currently the package is at 14.0.314.76I found that adding the following re-binding in the migration configuration code fixes this.
public Configuration()
{
AutomaticMigrationsEnabled = false;
//Adding this fixes the below error message...
//Spatial types and functions are not available for this provider because the assembly 'Microsoft.SqlServer.Types' version 10 or higher could not be found.
SqlProviderServices.SqlServerTypesAssemblyName = typeof(SqlGeography).Assembly.FullName;
}
@peternxyz solution was not enough for me, but pointed to the right direction. If You install SqlTypes from NuGet, You need not only to tell EF to use newer version of assembly, but also load the assembly. Therefor You should add not one, but two lines of code to migration configuration code:
c#
SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
SqlProviderServices.SqlServerTypesAssemblyName = typeof(SqlGeography).Assembly.FullName;
Most helpful comment
I found that adding the following re-binding in the migration configuration code fixes this.