使用entityframework操作sqlite数据库

首先要安装好,所需要的类库,通过NuGet来处理

 

http://stackoverflow.com/questions/28507904/vs-2015-sqlite-data-provider

安装这个http://system.data.sqlite.org/downloads

下载最新版  sqlite-netFx46-setup-bundle-x86-2015-1.0.103.0.exe

注意:在安装这个bundle的时候,必须勾选全部

 

 

https://erazerbrecht.wordpress.com/2015/06/11/sqlite-entityframework-6-tutorial/

 

 

如何插入数据,数据存在就更新

http://stackoverflow.com/questions/6966207/entityframework-insert-if-not-exist-otherwise-update

复制代码
void Method(object sender, DocumentLoadedEvent e)
{
    var data = e.ParsedData as Country[];
    using(var db = new DataContractEntities)
    {
        var names = data.Select(c => c.Name);
        var countriesInDb = db.Countries
            .Where(c => names.Contains(c.Name))
            .ToList(); // single DB query
        foreach(var country in data)
        {
            var countryInDb = countriesInDb
                .SingleOrDefault(c => c.Name == country.Name); // runs in memory
            if (countryInDb != null)
                db.Countries.ApplyCurrentValues(country);
            else
                db.Countries.AddObject(country);
        }
        db.SaveChanges();
     }
}
复制代码

 

注意事项:https://www.sqlite.org/autoinc.html

如果有一列为id,并且是primary key的话,这一列只会由sqlite自己控制

If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID. 

 

 

需要注意的是,每一个sqlite的数据表,都必须安排一个主键。

INTEGER PRIMARY KEY  必须是此种类型的,否则在插入以及更新的时候会遇到一堆乱七八糟的问题

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(665)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2015-09-22 comparison of floating point numbers with equality operator. possible loss of precision while rounding values
2015-09-22 Formatting is Specified but argument is not IFormattable
2015-09-22 Covariance and Contravariance in C#的搜索条件
2015-09-22 Covariance and Contravariance in C#, Part Two: Array Covariance
2015-09-22 Covariance and Contravariance in C#, Part One
2015-09-22 Co-variant array conversion from x to y may cause run-time exception
2014-09-22 如何将一个Winform嵌入到一个Control当中
点击右上角即可分享
微信分享提示