c# - 无法在配置中设置 ProviderService

标签 c# entity-framework entity-framework-6 ef-code-first

我正在尝试将我的 app.config ef 设置移动到代码中。

<configuration>
    <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    </configSections>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
            <parameters>
                <parameter value="System.Data.SqlServerCe.4.0" />
            </parameters>
        </defaultConnectionFactory>
        <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            <provider invariantName="System.Data.SqlServerCe.4.0"
                       type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
        </providers>
    </entityFramework>
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SqlServerCe.4.0" />
            <add name="Microsoft SQL Server Compact Data Provider 4.0" 
                 invariant="System.Data.SqlServerCe.4.0" 
                 description=".NET Framework Data Provider for Microsoft SQL Server Compact" 
                 type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
    </system.data>
</configuration>

目前我所能设置的是defaultConnectionFactory:

public class SqlServerCompactConfiguration : DbConfiguration
{
    public SqlServerCompactConfiguration()
    {
        SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"));
        SetProviderFactory("System.Data.SqlServerCe.4.0", new System.Data.SqlServerCe.SqlCeProviderFactory());

        SetProviderServices("System.Data.SqlServerCe.4.0", new System.Data.Entity.SqlServerCompact.SqlCeProviderServices());
        SetProviderServices("System.Data.SqlClient", new System.Data.Entity.SqlServer.SqlProviderServices());
    }
}

对于最后两行,我收到以下错误:

'SqlCeProviderServices' does not contain a constructor that takes 0 arguments'

我不知道要在这里添加什么...Intellisense 没有显示任何内容,当我使用 GoToDefinition 时类也是如此,没有构造函数...

最佳答案

通常这样的服务类被实现为单例。

例如,SqlProviderServices 类包含以下内容:

//
// Summary:
//     The Singleton instance of the SqlProviderServices type.
public static SqlProviderServices Instance { get; }

我现在无法检查,但我很确定 SqlProviderService 类有类似的东西。

关于c# - 无法在配置中设置 ProviderService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48063014/

相关文章:

c# - 创建方法信息 : Could not load file or assembly

c# - 调整捕捉 View 大小时没有 SizeChanged 事件

c# - 迁移 EF Core 中的顺序列

c# - EF6 Code First Key 不会自动递增

c# - EF6 Code First 具有通用存储库和依赖注入(inject)和 SoC

c# - 为什么 Select() 不将 IEnumerable<dynamic> 转换为 IEnumerable<StrongType>?

c# - 使用 Assembly.ReflectionOnlyLoadFrom 加载 WPF 项目引用的程序集时出现奇怪的 FileLoadException

c# - 什么时候发生错误迁移( Entity Framework )

c# - Entity Framework - 单个事务中的父子表更新

c# - 从 EF6 模型自动生成的 TT 文件渲染默认参数说明符不允许错误