Ef6: add-migration fails with DbGeography type.

Created on 21 Nov 2017  路  4Comments  路  Source: dotnet/ef6

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:

  1. Create SQL database with sys.geography data type
  2. Create new console application project
  3. R-click project, Add->New Item...
  4. Select "Data" from the left, and select "ADO.Net Entity Data Model"
  5. Select "Code First From Database"
  6. Specify the previously created SQL database
  7. Include all tables from database
  8. Build the application
  9. In Package Manager Console, enable migrations ("enable-migrations")
  10. In Package Manager Console, add a migration ("add-migration myMigration")
  11. See output for error.

I have tried:

  • Installing NuGet Package Microsoft.SqlServer.Types
  • On a machine with SQL Server installed
  • Suggestions from Stack Overflow article
closed-question

Most helpful comment

I 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;

        }

All 4 comments

@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.

  • try limiting yourself (at least initially) to using NuGet 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.76
  • restart visual studio after installing the types. For some reason I kept seeing the error you see, but when I reopened the project, it just worked and created the migration.

I 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;

Was this page helpful?
0 / 5 - 0 ratings