zoukankan      html  css  js  c++  java
  • 应用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)手动把安装包中的"packagesSystem.Data.SQLite.Core.1.0.103uild et45" 中的x64及x86复制到控制台项目的BinDebug目录中,如下所示。

     (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:UserspaulhuangDesktopSQLiteTest" providerName="System.Data.SQLite.EF6" />

    这时我们就可以书写LINQ代码来测试相应的功能了。

    1、插入数据

    2、修改数据

    关于通过EF访问SQLite的基本操作基本上到这里了,如果有不正确的地方欢迎指正。

  • 相关阅读:
    Eclipse汉化后怎么改回英文版 (中文 改 英文)
    解决android中Layout文件下的xml文件配好后,R类中不能自动生成相应代码
    Android SDK离线安装
    Windows环境下Android Studio v1.0安装教程
    Eclipse调试Bug的七种常用技巧
    博客开通了
    Android常见的按钮监听器实现方式
    用setTimeout实现在DOM上(通常是菜单栏)鼠标停留一段时间才执行相应的操作
    Javascript模块模式学习分享
    Oracle数据库逻辑存储结构管理
  • 原文地址:https://www.cnblogs.com/hehheai/p/6513789.html
Copyright © 2011-2022 走看看