Entity Framework 和 Sqlite

栏目: ASP.NET · 发布时间: 8年前

内容简介:Entity Framework 和 Sqlite

准备

EntityFramework 6

System.Data.SQLite.EF6

SQLite.CodeFirst

设置

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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
  <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" />
      <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" />
    </DbProviderFactories>
  </system.data>
</configuration>

读取DB

public class MyContext : DbContext
    {
        public MyContext(DbConnection conn)
            : base(conn, true)
        {

        }

使用CodeFirst写入DB

class MyContext : DbContext
    {
        public MyMdContext(DbConnection conn)
            :base(conn, true)
        {

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyContext>(modelBuilder);
            Database.SetInitializer(sqliteConnectionInitializer);

            base.OnModelCreating(modelBuilder);
        }

    }

数据映射

如果key类型为整形,必须使用Int64。Sql数据库中,INT为32位,INTEGER为64位

(1) EF创建表的时候,整形的key为Int64,即使定义的为int

(2) EF读取表的时候,若定义的key为INT,则用int,若INTEGER,则为Int64

故定义KEY,必须使用INTEGER。EF不会把数据库中int类型映射为Int64

输入插入

默认情况下,每次插入数据都会建立索引,故随着插入数据增多,会越来越慢。所以需要关闭此功能:

context.Configuration.AutoDetectChangesEnabled = false;

在context.SaveChanges的时候建立一次索引即可。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

小程序大能量

小程序大能量

肖月 / 人民邮电出版社 / 2018-11 / 49.80元

本书主要针对零基础的读者,详细讲解小程序的搭建以及小程序的运营等知识。全书共有6章。第 1章重点介绍了小程序诞生的原因以及小程序的发展历史;第 2章详细讲解了快速搭建小程序的方法;第3章向读者阐述了小程序和互联网运营的关系;第4章主要介绍了小程序运营的意义;第5章全面分析了打造爆款小程序的策略;第6章重点总结了小程序的营销推广策略。 本书可以作为对小程序感兴趣的个人以及企业的学习用书,帮助读者快速......一起来看看 《小程序大能量》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具