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

时间:2010-11-19 14:25:40

标签: c# entity-framework sqlite dbproviderfactories

相关问题: Running my application on another machine gives me an error

这就是我的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:\用户\ Sergio.Tapia \桌面\ DocumentScannerDanyly \ DocumentScannerDanyly \ BIN \调试\ 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框架数据提供商。

3 个答案:

答案 0 :(得分:11)

这是因为当您安装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>

答案 1 :(得分:3)

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

  

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

但解决这个问题的方法是在参考文献中为System.Data.SQLiteSystem.Data.SQLite.Linq设置“复制本地”为true。

我希望它也能帮到你。

答案 2 :(得分:2)

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

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

除非此文件与其他计算机上的位置完全相同,否则可能无法正常工作