![]() |
VOOZH | about |
Requires NuGet 5.0 or higher.
dotnet add package ErikEJ.EntityFramework.SqlServer --version 6.6.11
NuGet\Install-Package ErikEJ.EntityFramework.SqlServer -Version 6.6.11
<PackageReference Include="ErikEJ.EntityFramework.SqlServer" Version="6.6.11" />
<PackageVersion Include="ErikEJ.EntityFramework.SqlServer" Version="6.6.11" />Directory.Packages.props
<PackageReference Include="ErikEJ.EntityFramework.SqlServer" />Project file
paket add ErikEJ.EntityFramework.SqlServer --version 6.6.11
#r "nuget: ErikEJ.EntityFramework.SqlServer, 6.6.11"
#:package ErikEJ.EntityFramework.SqlServer@6.6.11
#addin nuget:?package=ErikEJ.EntityFramework.SqlServer&version=6.6.11Install as a Cake Addin
#tool nuget:?package=ErikEJ.EntityFramework.SqlServer&version=6.6.11Install as a Cake Tool
This Entity Framework 6 provider is a runtime replacement provider for the built-in SQL Server provider.
This provider depends on the modern Microsoft.Data.SqlClient ADO.NET provider, see my blog post here for why that can be desirable. One of the main reasons is the additional Azure Active Directory authentication methods that are now available.
The latest build of this package is available from NuGet
UPDATE An official Microsoft package based on this package has been published.
There are various ways to configure Entity Framework to use this provider.
You can register the provider in code using an attribute:
[DbConfigurationType(typeof(MicrosoftSqlDbConfiguration))]
public class SchoolContext : DbContext
{
public SchoolContext() : base()
{
}
public DbSet<Student> Students { get; set; }
}
If you have multiple classes inheriting from DbContext in your solution, add the DbConfigurationType attribute to all of them.
Or you can use the SetConfiguration method before any data access calls:
DbConfiguration.SetConfiguration(new MicrosoftSqlDbConfiguration());
Or you can add the following lines to your existing DbConfiguration class:
SetProviderFactory(MicrosoftSqlProviderServices.ProviderInvariantName, Microsoft.Data.SqlClient.SqlClientFactory.Instance);
SetProviderServices(MicrosoftSqlProviderServices.ProviderInvariantName, MicrosoftSqlProviderServices.Instance);
// Optional
SetExecutionStrategy(MicrosoftSqlProviderServices.ProviderInvariantName, () => new MicrosoftSqlAzureExecutionStrategy());
You can also use XML/App.Config based configuration:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<providers>
<provider invariantName="Microsoft.Data.SqlClient" type="System.Data.Entity.SqlServer.MicrosoftSqlProviderServices, ErikEJ.EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="SqlClient Data Provider"
invariant="Microsoft.Data.SqlClient"
description=".NET Framework Data Provider for SqlServer"
type="Microsoft.Data.SqlClient.SqlClientFactory, Microsoft.Data.SqlClient" />
</DbProviderFactories>
</system.data>
</configuration>
If you use App.Config with a .NET Core / .NET 5 or later app, you must remove the <system.data> section above and register the DbProviderFactory in code once:
DbProviderFactories.RegisterFactory(MicrosoftSqlProviderServices.ProviderInvariantName, Microsoft.Data.SqlClient.SqlClientFactory.Instance);
If you use an EDMX file, update the Provider name in the EDMX file:
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
<edmx:Runtime>
<edmx:StorageModels>
<Schema Namespace="ChinookModel.Store" Provider="Microsoft.Data.SqlClient" >
Also update the provider name inside the EntityConnection connection string - sample of working App.Config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<providers>
<provider invariantName="Microsoft.Data.SqlClient" type="System.Data.Entity.SqlServer.MicrosoftSqlProviderServices, ErikEJ.EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="SqlClient Data Provider" invariant="Microsoft.Data.SqlClient" description=".NET Framework Data Provider for SqlServer" type="Microsoft.Data.SqlClient.SqlClientFactory, Microsoft.Data.SqlClient" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="ChinookEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=Microsoft.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=Chinook;integrated security=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
In order to use the EDMX file with the Visual Studio designer, use Visual Studio 17.6 or later. Your project must be a "classic" .NET Framework project. Make sure to restore packages and build before opening the designer. You may need to restart Visual Studio after building for the first time. For designer issues, report here. An interesting hack for some designer issues is described here.
In order to use the provider in an existing solution, a few code changes are required (as needed).
using System.Data.SqlClient; ⇒ using Microsoft.Data.SqlClient;
using Microsoft.SqlServer.Server; ⇒ using Microsoft.Data.SqlClient.Server;
The following classes have been renamed to avoid conflicts with classes in the existing SQL Server provider:
SqlAzureExecutionStrategy ⇒ MicrosoftSqlAzureExecutionStrategy
SqlConnectionFactory ⇒ MicrosoftSqlConnectionFactory
SqlDbConfiguration ⇒ MicrosoftSqlDbConfiguration
SqlProviderServices ⇒ MicrosoftSqlProviderServices
SqlServerMigrationSqlGenerator ⇒ MicrosoftSqlServerMigrationSqlGenerator
SqlSpatialServices ⇒ MicrosoftSqlSpatialServices
Azure App Service with .NET Framework and connection strings configuration
If you use Azure App Service with .NET Framework and the connection strings configuration feature, you can encounter runtime issues, as the ProviderName connection string setting in this scenario is hardcoded to System.Data.SqlClient.
Solution is to use a derived MicrosoftSqlDbConfiguration class like this:
public class AppServiceConfiguration : MicrosoftSqlDbConfiguration
{
public AppServiceConfiguration()
{
SetProviderFactory("System.Data.SqlClient", Microsoft.Data.SqlClient.SqlClientFactory.Instance);
SetProviderServices("System.Data.SqlClient", MicrosoftSqlProviderServices.Instance);
SetExecutionStrategy("System.Data.SqlClient", () => new MicrosoftSqlAzureExecutionStrategy());
}
}
Then use this derived class in the code-based configuration described above.
EntityFramework.dll installed in GAC
If an older version of EntityFramework.dll is installed in the .NET Framework GAC (Global Assembly Cache), you might get this runtime error:
The 'PrimitiveTypeKind' attribute is invalid - The value 'HierarchyId' is invalid according to its datatype
Solution is to remove the .dll from the GAC, see this for more info
Please report any issues, questions and suggestions here
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 2 NuGet packages that depend on ErikEJ.EntityFramework.SqlServer:
| Package | Downloads |
|---|---|
|
SonicGD.EntityFramework.BulkExtensions
Bulk operations extension library for Entity Framework. |
|
|
CloudNimble.LinqPad.Drivers.EF6Core
A LINQPad 6+ driver for EF6 DbContexts using Microsoft.Data.SqlClient. |
This package is not used by any popular GitHub repositories.
Update to M.D.S. 5.2.1