简体   繁体   English

如何在SQLite和Visual Studio 2013中使用EF

[英]How to use EF with SQLite and Visual Studio 2013

I'd like to use SQLite with Entity Framework and Visual Studio 2013 Desktop Express. 我想将SQLite与Entity Framework和Visual Studio 2013 Desktop Express一起使用。

To do that i have right click on Reference and installed (with selected dependecies) "System.Data.SQLite(X86/x64)" (version 1.0.94.1) NuGet packages 要做到这一点,我右键单击引用并安装(带有选定的依赖)“System.Data.SQLite(X86 / x64)”(版本1.0.94.1)NuGet包

Now if i add a new element to my project and select ADO.NET Entity Data Model i don't see the SQLite Provider: i have only Microsoft SQL Server. 现在,如果我向我的项目添加一个新元素并选择ADO.NET实体数据模型,我没有看到SQLite提供者:我只有Microsoft SQL Server。

I have .NET Framework 4.5.1 and Windows 7. 我有.NET Framework 4.5.1和Windows 7。

This is my App.config 这是我的App.config

<?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>
 <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 </startup>
 <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>
      <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>
 <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
       <parameters>
           <parameter value="mssqllocaldb" />
       </parameters>
    </defaultConnectionFactory>
    <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>
</configuration>

You must install the DDEX provider from http://system.data.sqlite.org - but the Express versions do not allow installation of any extensions like DDEX providers. 您必须从http://system.data.sqlite.org安装DDEX提供程序 - 但Express版本不允许安装任何扩展,如DDEX提供程序。

You can install the new Visual Studio 2013 Community. 您可以安装新的Visual Studio 2013社区。 There, go to "Tools", "Extensions and Updates", the type "sqlite ddex" into the search box, and install it. 在那里,转到“工具”,“扩展和更新”,在搜索框中输入“sqlite ddex”类型,然后安装它。

While you have added the nuget packages for SQLite to your project, this is completely separate to the Visual Studio tooling that would be used to scaffold a data model. 虽然您已将SQLite的nuget包添加到项目中,但这与用于构建数据模型的Visual Studio工具完全分开。

You can install the runtime support for Visual Studio from http://system.data.sqlite.org/ - this should allow you to use the Visual Studio support for creating your model. 您可以从http://system.data.sqlite.org/安装Visual Studio的运行时支持 - 这应该允许您使用Visual Studio支持来创建模型。

I had to add this in the Providers section of Entity Framework: 我必须在Entity Framework的Providers部分添加它:

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

If you follow EricEJ's tutorial, this should then work for you. 如果您按照EricEJ的教程,这应该适合您。

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

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