繁体   English   中英

System.Data.SQLite 1.0.91.0和EF6.0.2

[英]System.Data.SQLite 1.0.91.0 and EF6.0.2

有没有人在Visual Studio 201#中使用新的System.Data.SQLite 1.0.91.0与Entity Framework 6一起工作? 如果你有,你是怎么做到的?

更新 - 2014年3月20日:System.Data.SQLite 1.0.92.0已经发布但我没有在VS2013中创建EDMX的运气:(我最终使用包管理器(因为EF6。#。#是新SQLite中的依赖项) NuGet包):

uninstall-package entityframework -force

重新启动VS2013并打开旧版EF5以获取VS2013以从现有数据库生成EDMX:

install-package entityframework -version 5.0.0

注意:这不是一个复杂的多表SQLite关系数据库测试,所以如果我使用多个导航(FK)关系的任何东西,我不确定会出现什么其他问题:/

回答EDMX /模型的答案:(更新 - 2014年3月2日)我找到了解决办法,但它不够一致,需要太多步骤才能将其视为有效的解决方案。 基本上:

  • 你制作所有的Class(es)文件,

  • 使用表建立与现有SQLite数据库的连接,

  • 修改web / app.config,如仍然出色的SQLite故障单( http://system.data.sqlite.org/index.html/tktview?name=7b8fd3ef77 )中的mistachkin所述,包括伪造cdsl / .ssdl /.msl件,和

  • 然后在重建项目之前在Empty EDMX中手动创建Designer中的所有实体模型,然后......

  • 右键单击实体并选择“从数据库更新”...

有时EF / VS2013会添加.tt / DbContesxt,有时则不会。 方式也'命中或错过':(

有和没有现有SQLite数据库的“Code First”的答案(基于PMCB,Drexter和Jimi的建议)。 但请注意,您无法在Visual Studio 2013中自动生成任何EF模型或EDMX文件[原文如此 - 概念架构定义语言(.CSDL),存储架构定义语言(.SSDL)和映射规范语言(.MSL)]。我做了不要尝试手动创建EDMX文件以查看它们是否适合EF,即使我这样做,在我看来,做所有手动创建/映射/更改/ XML编辑都会破坏基于实体的框架的整个目的/概念...

<connectionStrings>
    <add name="DogsContext" connectionString="Data Source=|DataDirectory|\dogs.s3db;" providerName="System.Data.SQLite" />
</connectionStrings>
<entityFramework>
    <providers>
       <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </providers>
</entityFramework>
<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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </DbProviderFactories>
</system.data>

这是测试类(请注意,我必须将DogID更改为Int64以使其与SQLite一起使用...):

using System.Data.Entity;

namespace WebApplication1.Models
{
    public class Dog
    {
        public Dog() { }

        public Int64 DogID { get; set; }
        public string DogName { get; set; }
    }
}

这是测试DbContext:

using System.Data.Entity;

namespace WebApplication1.Models
{
    public class DogsContext : DbContext
    {
        public DogsContext() : base() { }

        public DbSet<Dog> DogNames { get; set; }
    }
}

=============问题的原始细节==================

昨天发布了SQLite 1.0.91.0( http://system.data.sqlite.org/index.html/doc/trunk/www/news.wiki ),其中包含“添加对实体框架6的支持”。 包含的“自述文件”中有一个警告,您可以在web.config / app.config中添加以下内容:

<configuration>
<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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </DbProviderFactories>
</system.data>
</configuration>

如果在VS2013中使用NuGet添加“System.Data.SQLite(x86 / x64)”,现在将此行添加到web.config / app.config:

<entityFramework>
...
<providers>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
...
</entityFramework>

作为测试,我制作了一个工作的NET4.5.1 / MVC4 / EF5 / System.Data.SQlite 1.0.90应用程序的副本,并在其上运行PM:update-package。 它成功地将上面的行添加到我的web.config中,并添加了所需的“DbProviderFactories”部分。 重建并运行......未找到'找不到提供商'。 我在没有“DbProviderFactories”的情况下尝试它...在'找不到提供者'时失败。 我删除了新的“提供者”片段...未通过'找不到提供者'。 所以我尝试了一个新的项目,但有或没有Identity(OWIN),以防出现问题。 两者都没有工作......几乎没有想法,所以在Google / StackOverflow搜索之后问这里。

============ 2014年2月14日===============

不幸的是,更改的条目和众多变化在我的PC上无效...我在Win8.1 Pro x64上使用VS 2013 Pro。 我重新安装了System.Data.SQLite。 至少我收到了现有SQLite数据库和VS2013创建的新数据库的新错误(我可以访问它并看到它具有零表的正确结构):

"An error occurred connecting to the database. The database might be unavailable. An 
exception of type 'System.Data.Entity.Core.ProviderIncompatibleException' occurred. The 
error message is: Schema specified is not valid. Errors: StoreSchemaDefinition(2,64) : 
Error 0175: The ADO.NET provider with invariant name 'System.Data.SQLite.EF6' is either 
not registered in the machine or application config file, or could not be loaded. See the 
inner exception for details.'"

EF6和System.Data.SQLite(x86 / x64)都通过NuGet引入全新的MVC Web应用程序和Windows控制台应用程序。 两者都产生了上述错误......我开始怀疑我的VS3013可能出现了问题,但是当我可以使用MVC4 / SQLite / EF5时,我不想再花6个小时下载和修补它...

===== 2014年2月17日========

@Jimi - 这里没有运气。 我尝试使用新的MVC和Windows应用程序项目使用.NET 4.5和4.5.1。 我尝试使用EF6.0.0和EF6.0.2。 我尝试从NuGet添加“System.Data.SQLite(x86 / x64)”后,从安装System.Data.SQLite替换3x SQLite.Data.xxx.dll引用本地副本。 我还尝试了NuGet“System.Data.SQLite.MSIL”包,而不是将DBFactories添加到我尝试的各种版本的Web.Config和App.Config中。

我也尝试创建一个.NET 4.0 MVC4 Web应用程序,但看起来NuGet的“System.Data.SQLite(x86 / x64)”现在包含了对EF6的要求。 充满希望,我一起去尝试各种'保存/创建新连接/编辑web.config / etc',但它不起作用。 我已经放弃了,现在又回到了使用数据集的SQLite,因此我可以将Linq与System.Data.DataSetExtensions一起使用,使DataTables为“AsEnumerable”(或Java(jdbc),供那些希望在Android上使用数据库的脱机副本的人具有最小App界面的OS设备)。

========= 2014年2月19日=======

@Christian Sauer - 我没有让EF6和System.Data.SQLite(或System.Data.SQLite.EF6)一起工作,但......

我刚刚在http://system.data.sqlite.org/index.html/tktview?name=7b8fd3ef77上发现了一个更新,它说有一个更新的SQLite NuGet包:

mistachkin added on 2014-02-19 03:39:35:

All NuGet packages have been updated to 1.0.91.3.  Several fixes are included
that pertain to supporting Entity Framework 6.

我现在正在使用一个新项目(使用EF6.0.2的VS2013中的MVC Web项目和面向.NET 4.5.1的SQLite 1.0.91.3)测试它...

新的SQLite 1.0.91.3再没有运气。 web.config已更改为:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <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" />
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
   </DbProviderFactories>
 </system.data>

上班后我会尝试搞乱配置(如Jimi和PCMB所建议的那样)并发布任何调查结果。

我只是尝试重新安装32和64位版本的System.Data.SQLite(添加和不添加到GAC),但EF6不能与SQLite一起使用。 如果我尝试手动构建模型和映射,它会在我引用/尝试使用EF6时失败。 至于创建实体数据模型(无论是从现有的SQLite还是新的SQLite数据库生成),无论我对配置,连接还是提供者做什么,它都会失败/错误。

=========== 2014年2月22日=============

他们将System.Data.SQLite 1.0.91.3更新/回滚/回滚到1.0.91.0。 还有一张优秀的票证( http://system.data.sqlite.org/index.html/tktview?name=7b8fd3ef77 ),其中包含一些错误的建议。 这是app.config mistachkin推荐尝试(这个配置在我或之前不适用于我):

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
     <remove invariant="System.Data.SQLite.EF6" />
     <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="northwindEFEntities" connectionString="metadata=res://*/NorthwindModel.EF6.2013.csdl|res://*/NorthwindModel.EF6.2013.ssdl|res://*/NorthwindModel.EF6.2013.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=.\northwindEF.db&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <providers>
     <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </providers>
  </entityFramework>
</configuration>

在我尝试使用mistachkin的建议将dll添加到GAC(使用.NET“gacutil.exe”完成)之后......

我在今天工作了几个小时,然后才搞清楚。 NuGet包将相应的条目“System.Data.SQLite.EF6”添加到EntityFramework提供程序工厂中,但您必须删除或注释掉旧的“System.Data.SQLite”条目。 如果任何提供程序条目无效,EF上下文构建器将会崩溃,并且EF6中不支持旧的1.0.91 SQLite提供程序。

编辑:清理配置允许我的其他上下文对象(SQL Server)实例化,但它仍然不会正确实例化SQLite上下文对象。 内在的例外是

无法找到请求的.Net Framework数据提供程序。 它可能没有安装。

EDIT 2 / SOLUTION终于明白了! 我认为System.Data.SQLite中存在一个问题,即强制引用System.Data.SQLite提供程序名称而不是连接字符串中提供的值。 我能够通过在EntityFramework提供程序部分创建两个条目来解决这个问题,一个名为“System.Data.SQlite”,另一个名为“System.Data.SQLite.EF6”,但两个提供程序实际上都使用EF6提供程序。

<entityFramework>
...
<providers>
    <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</providers>

这是我的整个配置文件。 我正在使用SQLite 1.0.91运行EF 6.0.2。 我还没有尝试过模型生成器,但我的上下文对象工作正常,我通过上下文测试了insert / update / delete以及通过dbcontext直接测试SQL命令。 诀窍在entityFramework / providers部分。 我不得不复制EF6提供程序的SQLite提供程序条目,但使用旧的和新的不变名称。

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="electra.common.configuration.electraConfiguration" type="Electra.Common.Configuration.ElectraConfiguration, Electra.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <connectionStrings>
    <add name="DBContext_Server" 
        connectionString="server=sunset\sql2012;Integrated Security=SSPI;database=Electra;Pooling=true;max pool size=1000;min pool size=5;Connection Lifetime=30;connection timeout=15" 
        providerName="System.Data.SqlClient" />
    <add name="DBContext_ElectraPOS" 
        connectionString="Data Source=D:\Electra\ElectraWeb\PosEngine.Repository\ElectraPOS.db" 
        providerName="System.Data.SQLite.EF6" />
  </connectionStrings>
    <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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
            <remove invariant="System.Data.SQLite.EF6" />
            <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
        </DbProviderFactories>
    </system.data>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
        <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </providers>
  </entityFramework>
  <electra.common.configuration.electraConfiguration>
    <logging>
      <levels info="true" warning="true" error="true" debug="true" />
    </logging>
  </electra.common.configuration.electraConfiguration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="2.0.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

经过6个小时的试验/错误尝试后,我们开发人员确定了如何工作,我认为我已经设法在VS 2013上工作了。

按照以下步骤操作

  1. 如果您还没有安装System.Data.SQLite ,请安装它。
  2. 从NuGet中获取项目中的System.Data.SQLite(注意它在app.config中添加了一些东西)。
  3. 这是重要的部分。 保存解决方案并退出 VS 2013。
  4. 重新加载解决方案,现在您可以将ADO.NET实体数据模型添加到项目中。

每次要添加模型时,都必须退出VS 2013,并且必须使用“新建连接...”创建模型。

为什么它以这种方式工作,你说呢? 我说,宇宙以神秘的方式运作。

编辑:进一步研究它我发现,为了打开与模型的连接,您必须从<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />更改配置中的<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /><provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />DbProviderFactories必须位于system.data标记内。

只想分享我遇到同样问题的所有经验

我尝试使用System.Data.SQLite 1.0.94和EF6,并且在连接数据库方面存在一些问题。 我在VS 2013.4工作。 在安装了所有必需的软件包并将web.config作为pmbc建议后,我仍然遇到与System.Data.Entity.Core.ProviderIncompatibleException类型的连接和异常相同的问题。

在互联网上进行了一些额外的搜索后,我发现VS2013的这个附加组件可以与SQL Compact和SQLite数据库一起使用,这真的让我感到困惑: https ://visualstudiogallery.msdn.microsoft.com/0e313dfd-be80-4afb-b5e9-6e74d369f7a1

现在,我可以使用现有数据库和所有其他东西,使用现有的数据库/数据库创建新模型

希望这可以帮助

UPDATE

只需要一些帮助信息。 这是我生成的.config文件部分

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  <providers>
    <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.95.0, Culture=neutral" />
    <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.95.0, Culture=neutral" />
  </providers>
</entityFramework>
<system.data>
  <DbProviderFactories>
    <remove invariant="System.Data.SQLite" />
    <remove invariant="System.Data.SQLite.EF6" />
    <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
  </DbProviderFactories>
</system.data>

还要检查本地安装的System.Data.SQLite.dll库的版本号(默认情况下,它安装在GAC中)您当前项目中的GAC版本和软件包版本必须相同。 否则,您在启动应用程序时会遇到异常

它在VS 2010上使用控制台应用程序并首先执行代码。 无论如何这里是我的app.config,它基于Brice的EF6 SQLite教程( http://www.bricelam.net/2012/10/entity-framework-on-sqlite.html ):

<?xml version="1.0"?>
 <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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
 </configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0"/>
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</providers>
</entityFramework>
<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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</DbProviderFactories>
</system.data>
<connectionStrings>
 <add name="ChinookContext" connectionString="Data Source=|DataDirectory|Chinook_Sqlite_AutoIncrementPKs.sqlite" providerName="System.Data.SQLite"/>
</connectionStrings>
</configuration>

最后这个解决方案适合我。 DotNet Framewok = 4.5.1实体框架= 6.1.1

下载: sqlite-netFx451-setup-bundle-x86-2013-1.0.94.0.exe

<?xml version="1.0" encoding="utf-8"?>

<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 -->
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <!--<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />-->
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>

  <system.data>
    <!--
        NOTE: The extra "remove" element below is to prevent the design-time
              support components within EF6 from selecting the legacy ADO.NET
              provider for SQLite (i.e. the one without any EF6 support).  It
              appears to only consider the first ADO.NET provider in the list
              within the resulting "app.config" or "web.config" file.
    -->
    <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, Version=1.0.94.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />

      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="VelocityDBEntities" connectionString="metadata=res://*/AppData.Model1.csdl|res://*/AppData.Model1.ssdl|res://*/AppData.Model1.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=F:\VelocityPOS\VelocityDB.sqlite&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

我只是为System.Data.SQLite.EF6库设置了Copy local = True(在References - > System.Data.SQLite.EF6 - > Property中)它的工作原理

花了几个小时在互联网上挖掘,我在这里提出了一个有效的解决方案

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="NorthwindContext" connectionString="Data Source=Northwind.sl3" providerName="System.Data.SQLite.EF6" />
  </connectionStrings>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
    </DbProviderFactories>
  </system.data>
</configuration>

项目

这对我行得通 :

<entityFramework>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
</entityFramework>
<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" />
        <!--<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
        <remove invariant="System.Data.SQLite" />
        <remove invariant="System.Data.SQLite.EF6" />
        <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />-->
    </DbProviderFactories>
</system.data>

使用最新的nuget包(1.0.94.1): https ://www.nuget.org/packages/System.Data.SQLite/和SQLite工具:32位Windows(.NET Framework 4.5.1)的设置,位于http: //system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

我遇到了同样的错误,但其原因似乎没有被其他答案所覆盖 所以我想分享一下自己的案例。

在包含EDMX的程序集中,一切正常。 在客户端(执行)程序集中,我在运行时始终存在相同的错误(实体框架提供程序类型'System.Data.SQLite.EF6.SQLiteProviderServices,System.Data.SQLite.EF6'在ADO的应用程序配置文件中注册。无法加载具有不变名称'System.Data.SQLite.EF6'的.NET提供程序。)

我注意到,对于EDMX程序集,“Copy local”设置为true,但不执行程序集。 我修好了,没关系。 因此,如果您遇到此问题并且您在GAC中没有SQLite提供程序,请检查您的引用是否已为SQLite DLL启用了“copy local”

我失去了很长一段时间来检查提供商和其他一些东西,所以我希望它对其他人有用!

<providers>
  <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</providers>

这对我来说有一些内部错误,但它运行时可能会在EF喜欢为代码首先使用其他2个表__MigrationHistory和EdmMetadata之间做出区别。 我现在称它为微不足道但是如果你真的想要利用CodeFirst那些表必须手动创建我现在假设。

编辑 - 仅提供者列表中的条目,我删除了对SqlServer的所有引用...

Vs2013 Ultimate lastest SQLite Nuget。

我在包安装过程中遇到了权限问题,所以我收到了这个错误。 我解决了以管理员身份运行vs2013的问题

所以:

  1. 取消包装
  2. 关闭vs2013
  3. 以管理员身份再次运行(重要)
  4. 再次安装nugetpackage

最后我还在提供商中广告(如其他用户所建议的):

  <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />

这是适合我的解决方案。

安装System.Data.SQLite包并确保db factories部分如下所示。

<DbProviderFactories>
  <remove invariant="System.Data.SQLite" />
  <remove invariant="System.Data.SQLite.EF6" />
  <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
  <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
</DbProviderFactories>

通过从Nuget包管理器安装最新的SQLite,我在安装SQLite包时遇到了错误(在处理需要edmx而不是sqlite DB的项目时)。

第1步:仔细阅读信息。 它可以揭示错误消息试图传达的内容。

例如:

An error occurred while connecting to the database. The database might be unavailable. An exception of type 'System.InvalidCastException' occurred. 

The error message is: '[A]System.Data.SQLite.SQLiteConnection cannot be cast to [B]System.Data.SQLite.SQLiteConnection. 

Type A originates from 'System.Data.SQLite, Version=1.0.105.1, Culture=neutral, PublicKeyToken=db937bc2d44ff139' in the context 'Default' 
at location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data.SQLite\v4.0_1.0.105.1__db937bc2d44ff139\System.Data.SQLite.dll'. 

Type B originates from 'System.Data.SQLite, Version=1.0.106.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' in the context 'LoadNeither' 
at location 'C:\Users\Ganesh Kamath\AppData\Local\Microsoft\VisualStudio\14.0\ProjectAssemblies\og1mcjvn01\System.Data.SQLite.dll'.'.

阅读本文后,我意识到我已选择版本1.0.106.0进行升级。

本质上,该消息表明它知道版本1.0.105.1但不知道版本1.0.106.0

步骤2:通过在Visual Studio中手动选择Nuget Package for Solution选项中的出现来删除导致问题的版本。

工具> Nuget包管理器>管理解决方案的Nuget包...

在此输入图像描述

步骤3:使用Nuget命令行安装系统理解的SQLite版本。

在上面的示例中,版本是1.0.105.1

工具> Nuget包管理器>包管理器控制台

在此输入图像描述

这需要的命令如下所示:

Install-Package System.Data.SQLite -Version 1.0.105.1 
Install-Package System.Data.SQLite.Core -Version 1.0.105.1 
Install-Package System.Data.SQLite.EF6 -Version 1.0.105.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM