码迷,mamicode.com
首页 > 数据库 > 详细

应用EF访问SQLite数据

时间:2017-03-07 11:35:58      阅读:233      评论:0      收藏:0      [点我收藏+]

标签: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数据

标签:ide   count()   mis   应用程序   packages   .net   其他   line   测试表   

原文地址:http://www.cnblogs.com/hehheai/p/6513789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!