将配置添加到 App.config 文件时出错

发布于 2024-10-03 04:56:32 字数 2402 浏览 0 评论 0原文

相关问题: 在另一台计算机上运行我的应用程序会出现错误

这就是我的 App.config 文件的样子:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DocumentsDBEntities" connectionString="metadata=res://*/Documents.csdl|res://*/Documents.ssdl|res://*/Documents.msl;provider=System.Data.SQLite;provider connection string=&quot;data source=C:\Users\Sergio.Tapia\Desktop\DocumentScannerDanyly\DocumentScannerDanyly\DocumentsDB.sqlite&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" />
  </startup>
  <appSettings>
    <add key="Username" value="administrador"/>
    <add key="Password" value="123456"/>
  </appSettings>
</configuration>

在我的开发计算机上运行它可以工作,但是当部署到另一台计算机时,我收到数据提供程序错误。 (参见上面的相关问题)。

建议的解决方案是将其添加到 App.config 文件中:

<system.data>
        <DbProviderFactories>
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

当我将其添加到 App.config 文件中时,在 Visual Studio 2010 中启动应用程序时出现此错误:

创建时发生错误 配置节处理程序 system.data:列“InvariantName”是 被限制为唯一的。价值 “System.Data.SQLite”已经 展示。 (C:\Users\Sergio.Tapia\Desktop\DocumentScannerDanyly\DocumentScannerDanyly\bin\Debug\DocumentScannerDanyly.vshost.exe.Config 第 13 行)

关于这个错误是什么有什么建议吗?另外,由于 .sqlite 文件的位置相对于它的安装位置,我是否必须将 AppConfig 文件中的 connectionString 更改为更动态的内容?

感谢您的帮助。

编辑:

当我按照此处某人的建议将其添加到配置中时,出现错误:

<system.data>
        <DbProviderFactories>
                <clear />
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

找不到或加载已注册的 .Net 框架数据提供程序。

Related question:
Running my application on another machine gives me an error

This is how my App.config file looks like:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DocumentsDBEntities" connectionString="metadata=res://*/Documents.csdl|res://*/Documents.ssdl|res://*/Documents.msl;provider=System.Data.SQLite;provider connection string="data source=C:\Users\Sergio.Tapia\Desktop\DocumentScannerDanyly\DocumentScannerDanyly\DocumentsDB.sqlite"" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" />
  </startup>
  <appSettings>
    <add key="Username" value="administrador"/>
    <add key="Password" value="123456"/>
  </appSettings>
</configuration>

Running this on my dev machine works, but when deploying to another computer, I get an Data Provider error. (see related question above).

The suggested solution was to add this to the App.config file:

<system.data>
        <DbProviderFactories>
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

When I add that to the App.config file, I get this error when launching the application in Visual Studio 2010:

An error occurred creating the
configuration section handler for
system.data: Column 'InvariantName' is
constrained to be unique. Value
'System.Data.SQLite' is already
present.
(C:\Users\Sergio.Tapia\Desktop\DocumentScannerDanyly\DocumentScannerDanyly\bin\Debug\DocumentScannerDanyly.vshost.exe.Config
line 13)

Any suggestion on what this error is? Also, since the location of the .sqlite file is relative to where it is installed, do I have to change the connectionString in the AppConfig file to something more dynamic?

Thanks for the help.

EDIT:

When I add this to the config as suggested by someone here, I get an error:

<system.data>
        <DbProviderFactories>
                <clear />
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

Failed to find or load the registered
.Net Framework Data Provider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写下不归期 2024-10-10 04:56:32

这是因为当您安装 SqlLite 时,它​​会更新您的 machine.config:

<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>

因为您没有在安装了 SqlLite 的计算机上运行,​​所以 DbProviderFactories 不知道 SqlLite。

在目标计算机上安装 SqlLite 或将其添加到您的配置中:

<system.data>
        <DbProviderFactories>
                <clear />
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

这将阻止您与 machine.config 发生冲突,并允许它在目标计算机上运行。如果您使用任何其他 Sql 数据提供程序,您还需要添加它。

编辑

如果清除不起作用尝试:

<system.data>
        <DbProviderFactories>
                <remove invariant="System.Data.SQLite" />
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

This is because when you install SqlLite it updates your machine.config with:

<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>

Because you are not running on a machine where SqlLite has been installed the DbProviderFactories does not know about SqlLite.

Either install SqlLite on your destination machine or add this to your config:

<system.data>
        <DbProviderFactories>
                <clear />
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

This will stop you clashing with your machine.config, and allow it work on your target machine. If you are using any other Sql data provider you would also need to add that as well.

EDIT

If clear isn't working try:

<system.data>
        <DbProviderFactories>
                <remove invariant="System.Data.SQLite" />
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>
第几種人 2024-10-10 04:56:32

我遇到了同样的问题,当添加上述解决方案时,我得到了

无法找到或加载已注册的 .Net Framework 数据提供程序。

但解决这个问题的方法是在参考文献中将 System.Data.SQLite 和 System.Data.SQLite.Linq 的“Copy Local”设置为 true。

我希望它也能帮助你。

I had the same exact problem, when adding the above solution I got

Failed to find or load the registered .Net Framework Data Provider.

but what solved it was to set "Copy Local" to true for System.Data.SQLite and for System.Data.SQLite.Linq in the References.

I hope it'll help you too.

信愁 2024-10-10 04:56:32

问题很可能是您在连接字符串中硬编码了桌面路径:

C:\Users\Sergio.Tapia\Desktop\DocumentScannerDanyly\DocumentScannerDanyly\DocumentsDB.sql

除非此文件在另一台计算机上的位置完全相同,否则它很可能无法工作

The problem is most likely that you hard code a path to your desktop in your connection string:

C:\Users\Sergio.Tapia\Desktop\DocumentScannerDanyly\DocumentScannerDanyly\DocumentsDB.sql

Unless this file is present with the exact same location on the other machine, chances iare it won't work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文