Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does EF 6 support SQL Server 2014 Types?

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?

like image 501
Mansfield Avatar asked Sep 29 '15 03:09

Mansfield


People also ask

Is Entity Framework 6 still supported?

Versions 6.0, 6.1, 6.2, and 6.3 are no longer supported. Although Entity Framework 6. x is still supported, it is no longer being developed and will only receive fixes for security issues.

Does .NET 6 Support Entity Framework?

NET Framework, as Entity Framework 6 doesn't support . NET Core. If you need cross-platform features you will need to upgrade to Entity Framework Core. The recommended way to use Entity Framework 6 in an ASP.NET Core application is to put the EF6 context and model classes in a class library project that targets .

When would you use EF6 vs EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Does SQL Server use .NET framework?

NET Framework 4.8 | Free official downloads (microsoft.com). SQL Server 2014 and SQL Server 2012 use . Net Framework 3.5 SP1, which is supported till 2029, so this retirement doesn't impact them.


1 Answers

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";
like image 196
Ulugbek Umirov Avatar answered Sep 29 '22 00:09

Ulugbek Umirov