应用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的基本操做基本上到这里了,若是有不正确的地方欢迎指正。

相关文章
相关标签/搜索