應用EF訪問SQLite數據


創建項目,應用EF訪問SQLite

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的基本操作基本上到這里了,如果有不正確的地方歡迎指正。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM