應用EF訪問SQLite數據

建立項目,應用EF訪問SQLite數據庫

一、建立項目app

項目結構初始結構以下圖所示,Netage.Data.SQLite 類庫項目用於定義訪問數據的接口和方法,Netage.SQLiteTest.UI 控制檯項目引用 Netage.Data.SQLite 類庫,調用其相應的方法來訪問數據。
ide

二、在項目中加入SQLite類庫工具

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

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

三、定義數據上下文及相應的實體類spa

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; }
}

四、修改配置文件code

在安裝 "System.Date.SQLite(x86/x64)" 時,會默認在類庫項目的app.config文件中加入一些配置信息,可是因爲如今咱們須要經過控制檯項目來訪問類庫項目的方法來訪問數據,因此須要把配置信息從類庫項目複製到控制檯項目中。並將app.config文件從類庫項目中刪除。控制檯項目中的配置信息以下所示:orm

<?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",因此須要中配置文件中加入鏈接字符串信息。xml

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

五、在控制檯項目中經過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建立數據庫及表結構

一、建立數據庫,指定數據庫文件名及文件存放位置

二、建立表結構,添加列,並設置列的屬性

三、測試表建立完成,能夠再本身嘗試去操做這個工具的其餘功能,這裏再也不細說。

應用EF訪問SQLite數據庫數據

由於上面已經將數據庫文件存放到了別的位置 ,能夠須要修改配置文件以特定新建立的數據庫。

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

這時咱們就能夠書寫LINQ代碼來測試相應的功能了。

一、插入數據

 

二、修改數據

 

 

關於經過EF訪問SQLite的基本操做基本上到這裏了,若是有不正確的地方歡迎指正。

相關文章
相關標籤/搜索