实体框架 6 提供程序Entity Framework 6 Providers

备注

仅限 EF6 及更高版本 - 此页面中讨论的功能、API 等已引入实体框架 6。EF6 Onwards Only - The features, APIs, etc. discussed in this page were introduced in Entity Framework 6. 如果使用的是早期版本,则部分或全部信息不适用。If you are using an earlier version, some or all of the information does not apply.

实体框架现在正在开源许可证下开发,EF6 及更高版本不会作为 .NET Framework 的一部分提供。The Entity Framework is now being developed under an open-source license and EF6 and above will not be shipped as part of the .NET Framework. 这样做有许多好处,但也需要针对 EF6 程序集重建 EF 提供程序。This has many advantages but also requires that EF providers be rebuilt against the EF6 assemblies. 这就意味着 EF5 及以下版本的 EF 提供程序在重建之前不能用于 EF6。This means that EF providers for EF5 and below will not work with EF6 until they have been rebuilt.

哪些提供程序可用于 EF6?Which providers are available for EF6?

已知的针对 EF6 重建的提供程序包括:Providers we are aware of that have been rebuilt for EF6 include:

请注意,此列表并不表示给定提供程序的功能级别或支持情况,只表示已构建了 EF6。Note that inclusion in this list does not indicate the level of functionality or support for a given provider, only that a build for EF6 has been made available.

注册 EF 提供程序Registering EF providers

从实体框架 6 开始,可以使用基于代码的配置或在应用程序的配置文件中注册 EF 提供程序。Starting with Entity Framework 6 EF providers can be registered using either code-based configuration or in the application’s config file.

配置文件注册Config file registration

在 app.config 或 web.config 中注册 EF 提供程序的格式如下:Registration of the EF provider in app.config or web.config has the following format:

    <entityFramework>
       <providers>
         <provider invariantName="My.Invariant.Name" type="MyProvider.MyProviderServices, MyAssembly" />
       </providers>
    </entityFramework>

请注意,如果从 NuGet 安装 EF 提供程序,则通常 NuGet 包会自动将此注册添加到配置文件中。Note that often if the EF provider is installed from NuGet, then the NuGet package will automatically add this registration to the config file. 如果向非应用启动项目安装 NuGet 包,则可能需要将该注册复制到启动项目的配置文件中。If you install the NuGet package into a project that is not the startup project for your app, then you may need to copy the registration into the config file for your startup project.

此注册中的“invariantName”是用于标识 ADO.NET 提供程序的相同固定名称。The “invariantName” in this registration is the same invariant name used to identify an ADO.NET provider. 它是 DbProviderFactories 注册中的“invariant”属性,是连接字符串注册中的“providerName”属性。This can be found as the “invariant” attribute in a DbProviderFactories registration and as the “providerName” attribute in a connection string registration. 要使用的固定名称也应包含在该提供程序的文档中。The invariant name to use should also be included in documentation for the provider. 固定名称的示例:SQL Server 为“System.Data.SqlClient”,SQL Server Compact 为“System.Data.SqlServerCe.4.0”。Examples of invariant names are “System.Data.SqlClient” for SQL Server and “System.Data.SqlServerCe.4.0” for SQL Server Compact.

此注册中的“类型”是派生自“System.Data.Entity.Core.Common.DbProviderServices”的提供程序类型的程序集限定名称。The “type” in this registration is the assembly-qualified name of the provider type that derives from “System.Data.Entity.Core.Common.DbProviderServices”. 例如,用于 SQL Compact 的字符串是“System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact”。For example, the string to use for SQL Compact is “System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact”. 此处要使用的类型应包含在该提供程序的文档中。The type to use here should be included in documentation for the provider.

基于代码的注册Code-based registration

从实体框架 6 开始,可在代码中指定整个应用程序的 EF 配置。Starting with Entity Framework 6 application-wide configuration for EF can be specified in code. 有关完整的详细信息,请参阅实体框架基于代码的配置__。For full details see Entity Framework Code-Based Configuration. 使用基于代码的配置注册 EF 提供程序的常规方法是,创建一个派生自 System.Data.Entity.DbConfiguration 的新类,并将其放置在与 DbContext 类相同的程序集中。The normal way to register an EF provider using code-based configuration is to create a new class that derives from System.Data.Entity.DbConfiguration and place it in the same assembly as your DbContext class. 然后,DbConfiguration 类应在其构造函数中注册该提供程序。Your DbConfiguration class should then register the provider in its constructor. 例如,要注册 SQL Compact 提供程序,DbConfiguration 类如下所示:For example, to register the SQL Compact provider the DbConfiguration class looks like this:

    public class MyConfiguration : DbConfiguration
    {
        public MyConfiguration()
        {
            SetProviderServices(
                SqlCeProviderServices.ProviderInvariantName,
                SqlCeProviderServices.Instance);
        }
    }

在此代码中,“SqlCeProviderServices.ProviderInvariantName”为 SQL Server Compact 提供程序的固定名称字符串(“System.Data.SqlServerCe.4.0”)提供了便利,SqlCeProviderServices.Instance 会返回 SQL Compact EF 提供程序的单一实例。In this code “SqlCeProviderServices.ProviderInvariantName” is a convenience for the SQL Server Compact provider invariant name string (“System.Data.SqlServerCe.4.0”) and SqlCeProviderServices.Instance returns the singleton instance of the SQL Compact EF provider.

如果未提供我需要的提供程序怎么办?What if the provider I need isn’t available?

如果该提供程序适用于以前版本的 EF,则建议联系提供程序的所有者并要求他们创建 EF6 版本。If the provider is available for previous versions of EF, then we encourage you to contact the owner of the provider and ask them to create an EF6 version. 应包含对 EF6 提供程序模型的文档的引用。You should include a reference to the documentation for the EF6 provider model.

可以自己编写提供程序吗?Can I write a provider myself?

当然可以自己创建 EF 提供程序,但不应把这项任务考虑的太简单。It is certainly possible to create an EF provider yourself although it should not be considered a trivial undertaking. 可以从上面关于 EF6 提供程序模型的链接开始着手。The the link above about the EF6 provider model is a good place to start. 你可能还会发现,从 EF 开源代码库中包含的 SQL Server 和 SQL CE 提供程序的代码着手或用作参考会很有用。You may also find it useful to use the code for the SQL Server and SQL CE provider included in the EF open source codebase as a starting point or for reference.

请注意,从 EF6 开始,EF 提供程序与基础 ADO.NET 提供程序的耦合紧密程度降低。Note that starting with EF6 the EF provider is less tightly coupled to the underlying ADO.NET provider. 这使得编写 EF 提供程序更轻松,且无需编写或包装 ADO.NET 类。This makes it easier to write an EF provider without needing to write or wrap the ADO.NET classes.