本文介绍了API修改machine.config文件 - “DbProviderFactories”部分只能每个配置文件一旦出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在客户机上遇到了以下错误:

I've recently encountered the following error on a client machine:

在'DbProviderFactories部分​​只能出现一次,每个配置文件。

The 'DbProviderFactories' section can only appear once per config file.

似乎机器配置包含重复DbProviderFactories元素

It appears that the machine config contains a duplicate DbProviderFactories element.

<system.data>
    <DbProviderFactories>
        <add name="IBM DB2 for i .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for IBM i" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26" />
    </DbProviderFactories>
    <DbProviderFactories />
</system.data>

手动删除这个多余的元素解决问题,而我们的软件可以运行。但是,它已经要求我们尽量和工作围绕这或许忽略里面我们自己的app.config重复的条目。这是因为很多客户可能有同样的问题,我们不能修改每个人的配置文件。

Manually removing this extra element fixes the problem, and our software can run. However, it has been requested that we try and work around this by perhaps ignoring the duplicate entry inside our own app.config. This is because many clients might have the same issue, and we can't modify everyone's config file.

我试着加入&LT;清/&gt;在System.Data这部分里面元素,有希望替代那里的东西已经在machine.config。但是,这是行不通的。

I've tried adding a <clear/> element inside the system.data section, to hopefully override what's there already in the machine.config. However, this does not work.

例如

<system.data>
    <clear />
    <DbProviderFactories>
      <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>

有没有一种方法以编程方式忽略重复DbProviderFactories元素?

Is there a way to programmatically ignore the duplicate DbProviderFactories element?

有没有一个API存在,让您可以修改本机配置?

Does an API exist to allow you to modify the machine config?

谁能帮助,或推荐的解决方案?

Can anyone help, or recommend a solution?

亲切的问候

推荐答案

最近我碰到过同样的问题,而试图用SqlServerCe与实体框架。

I have recently come across this same problem whilst trying to use SqlServerCe with Entity Framework.

我设法解决此问题,通过使用 $ C $基于C语言的配置在实体框架6。

I have managed to work around this issue by using the Code-Based configuration features available in Entity Framework 6.

所有你需要做的就是删除&LT; entityframeowrk&GT; &LT; System.Data这&GT; 从标签你的的app.config 键,包括类似于在您的项目下列类:

All you need to do is remove the <entityframeowrk> and <system.data> tags from your app.config and include a class similar to the following in your project:

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.SqlServerCompact;
using System.Data.SqlServerCe;

namespace Data
{
    public class DatabaseConfiguration : DbConfiguration
    {
        public DatabaseConfiguration()
        {
            SetExecutionStrategy("System.Data.SqlServerCe.4.0", () => new DefaultExecutionStrategy());
            SetProviderFactory("System.Data.SqlServerCe.4.0", new SqlCeProviderFactory());
            SetProviderServices("System.Data.SqlServerCe.4.0", SqlCeProviderServices.Instance);
        }
    }
}

实体框架将自动选择这为你使用它。的注意事项需要注意的是,在出现此类中的的app.config 将覆盖任何配置。

Entity framework will then automatically pick this up for you and use it. A note to watch out for is that configuration in the app.config will overwrite anything that appears in this class.

另外,如果你不想包含这个类在数据层,看到移动DbConfiguration部分的这里

Also if you do not want to include this class in your data layer, see the Moving DbConfiguration section here.

这篇关于API修改machine.config文件 - “DbProviderFactories”部分只能每个配置文件一旦出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 03:11