Entity Framework Sqlite database: NotSupportedException



我按照本教程启动了一个使用 Entity Framework 5 和 SQLite db 的项目:http://brice-lambson.blogspot.be/2012/10/entity-framework-on-sqlite.html

但是,在我的应用程序中,我有多个项目:

  • Project.UI:前端逻辑
  • 项目模型:POCO 类
  • Project.DataAccess:数据访问逻辑:实体框架项目

现在我得到以下异常:

System.NotSupportedException was unhandle HResult=-2146233067
消息 = 无法确定类型连接的提供程序名称 'System.Data.SqlClient.SqlConnection'. 源=实体框架

我按照教程中设置了所有内容,将EF5和System.Data.SQLite安装到所有项目中。这是我来自主项目的 App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
    <connectionStrings>
      <add name="EasyInvoiceContext" connectionString="Data Source=|DataDirectory|EasyInvoice_v1.sqlite" providerName="System.Data.SQLite" />
    </connectionStrings>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

有人知道我做错了什么吗?

我的项目的更多源代码可以在github上找到: https://github.com/SanderDeclerck/EasyInvoice

尝试使用完整的限定程序集名称,同时在配置中添加DBProviderFactory..类似的东西。

<add name="SQLite Data Provider" 
     invariant="System.Data.SQLite" 
     description=".Net Framework Data Provider for SQLite" 
     type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />

我找到了解决方案,在 App.config 中,连接字符串应该是配置的子项,而不是 system.data 的子项。

这是固定的App.config(经过测试并正常工作):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="EasyInvoiceContext" connectionString="Data Source=|DataDirectory|Database/EasyInvoice_v1.sqlite" providerName="System.Data.SQLite" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

相关内容

  • 没有找到相关文章

最新更新