首先要安装好,所需要的类库,通过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 必须是此种类型的,否则在插入以及更新的时候会遇到一堆乱七八糟的问题