應用EF訪問SQLite資料

來源:互聯網
上載者:User

標籤:ide   count()   mis   應用程式   packages   .net   其他   line   測試表   

1、建立項目

項目結構初始結構如所示,Netage.Data.SQLite 類庫項目用於定義訪問資料的介面和方法,Netage.SQLiteTest.UI 控制台項目引用 Netage.Data.SQLite 類庫,調用其相應的方法來訪問資料。

2、在項目中加入SQLite類庫

 右鍵 Netage.Data.SQLite 項目,選擇"Manage Nuget Packages"菜單,在輸入框中輸入"System.Data.SQLite",查詢到"System.Data.SQLite(x86/x64)",並單擊安裝。:

安裝完成後,在"Netage.Data.SQLite"項目中就引入了相應的類庫。

3、定義資料內容及相應的實體類

public class MyContext : DbContext{    public DbSet<Person> Persons { get; set; }    public MyContext()        : base("SqliteTest")    {    }}
public class Person{    public int Id { get; set; }    public string Name { get; set; }    public DateTime BirthDay { get; set; }}

4、修改設定檔

在安裝 "System.Date.SQLite(x86/x64)" 時,會預設在類庫項目的app.config檔案中加入一些配置資訊,但是由於現在我們需要通過控制台項目來訪問類庫項目的方法來訪問資料,所以需要把配置資訊從類庫項目複製到控制台項目中。並將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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />    </configSections>    <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>    <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></configuration>

這些資訊都是在安裝SQLite時自動設定的,由於我們在MyContext定義資料連線字串名稱為"SqliteTest",所以需要中設定檔中加入連接字串資訊。

<connectionStrings>    <add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />  </connectionStrings>

5、在控制台項目中通過MyContext訪問資料

(1) 引用 "Netage.Data.SQLite"類庫項目

(2) 引入其他DLL

 

(3) 通過MyContext訪問資料

class Program{    static void Main(string[] args)    {        using (var context = new MyContext())        {            Console.WriteLine(context.Persons.Count());        }        Console.WriteLine("運行結束");        Console.ReadKey();    }}

(4)運行控制台項目

(5)手動把安裝包中的"packages\System.Data.SQLite.Core.1.0.103\build\net45" 中的x64及x86複製到控制台項目的Bin\Debug目錄中,如下所示。

 

 (6) 再次運行控制台項目

(7)再次修改設定檔,修改的配置已經被紅色部分標識,如下所示:

<?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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  </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" />      <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.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" />      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />    </providers>  </entityFramework>  <connectionStrings>    <add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />  </connectionStrings></configuration>

(8) 再次運行應用程式,如下所示:

這是因為EF SQLite不支援CodeFirst模式,因為到目前為止,我們還沒有建立資料庫及表結構,所以才有此錯誤。

通過SQLite Expert建立資料庫及表結構

1、建立資料庫,指定資料庫檔案名及檔案存放位置

2、建立表結構,添加列,並設定列的屬性

3、測試表建立完成,可以再自己嘗試去操作這個工具的其他功能,這裡不再細說。

應用EF訪問SQLite資料庫資料

因為上面已經將資料庫檔案存放到了別的位置 ,可以需要修改設定檔以特定新建立的資料庫。

 <add name="SqliteTest" connectionString="data source=C:\Users\paulhuang\Desktop\SQLiteTest" providerName="System.Data.SQLite.EF6" />

這時我們就可以書寫LINQ代碼來測試相應的功能了。

1、插入資料

 

2、修改資料

 

 

關於通過EF訪問SQLite的基本操作基本上到這裡了,如果有不正確的地方歡迎指正。

應用EF訪問SQLite資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.