Does EF 6 support SQL Server 2014 Types?

.net c# entity-framework entity-framework-6 sql-server-2014

Question

I have an asp.net mvc 5 web api application where I need to convert a SqlGeography instance into a DbGeography instance for querying with Entity Framework 6. I'm using the following code to do so:

SqlGeography geo = SqlGeography.STGeomFromText(chars, Constants.SRID);
DbGeography dbGeo = DbSpatialServices.Default.GeographyFromProviderValue(geo);

The call to GeographyFromProviderValue throws the following exception:

The specified provider value is not compatible with this spatial services implementation. A value is required of type 'Microsoft.SqlServer.Types.SqlGeography, Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'.
Parameter name: providerValue

Sure enough, my SqlGeography instance is coming from the SQL Server 2014 types assembly (Microsoft.SqlServer.Types, Version 12.0.0.0).

Digging into the Entity Framework source code shows this method to be the culprit:

//EntityFramework.SqlServer.dll(6.0.0.0) System.Data.Entity.SqlServer.SqlTypesAssemblyLoader
public SqlTypesAssemblyLoader(IEnumerable<string> assemblyNames = null)
{   
    this._preferredSqlTypesAssemblies = (assemblyNames ?? ((IEnumerable<string>)new string[]
    {
        "Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91",
        "Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
    }));
        this._latestVersion = new Lazy<SqlTypesAssembly>(new Func<SqlTypesAssembly>(this.BindToLatest), true);
    }
}

As you can see, the types assembly for SQL Server 2014 is not included. Does this mean that Entity Framework 6 does not support types from SQL Server 2014?

Obviously I could find the Types assembly for SQL Server 2012 and use that instead, but I'd rather not have to. Is there any other way around this issue?

1
8
9/29/2015 4:37:09 AM

Accepted Answer

You can set SQL Server types assembly via SqlProviderServices.SqlServerTypesAssemblyName static property. So, put in startup code the following:

SqlProviderServices.SqlServerTypesAssemblyName = "Microsoft.SqlServer.Types, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91";
6
9/29/2015 5:28:15 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow