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?
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";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.