Sqlite之Code First(EF6),sqliteef6
- Sqlite with Entity Framework Code First
- 使用类库
- 配置
- 类设计
- 使用
- 更加详细的资料
- 源码下载 SQLite_EF6
Sqlite with Entity Framework Code First
来个高大上的标题骗骗顾客,来了就看一下嘛
关于SQLite的CodeFirst,相信你看过很像的DBfirst例子,然后感觉十分坑,想放弃,又不甘心,如果是这样的话,那就再给我坑一下吧,哈哈哈
我认为是符合我标准的codefirst,如果你信的话。
使用类库
直接上个截图,大家都可以在nuget上引用进来,不过我提两个要注意的地方:
1.System.Data.SQLite.EF6.Migrations 这个类库用最新的吧,如果有更新的话,这是数据库迁移工具,(sqlite本身是没有数据库迁移 MigrationSqlGenerator 功能);2.EntityFramework,这个可以考虑用我现在的版本,我当时用的太高,有点问题,在数据连接时候,如果最新的EntityFramework有问题的话,那就用我这个V6.13吧。
配置
需要在App.config 下配置entityFramework的providers,
还有system.data下的****DbProviderFactories,如果不想看到sqlserver的那个,可以删掉的。
<providers>
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<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>
类设计
需要4个类:
1. Entity 类(实体类) //我这个实体类有点复杂了,中间涉及到了一个View的一层转换,大家别介意了,纯属满足自己的逻辑
2. Mapping 类(实体配置) //这个调用ef的模型类,配合迁移操作
3. DbContext 类(数据库操作) //外部访问的类,可使用linq和lambda操作
4. Configuration 类(设置信息)//配置迁移信息
这里就不列出代码,大家可以下载源码查看(在底部)
使用
这里我只举了一个很简单添加操作例子,有关linq,lambda的操作都没有用到,大家可以自己尝试一下,
别嫌我不直接上代码,有些东西必须的自己亲自动手过了,才会懂得,好好玩吧。
using (var db = new SQLiteDB())
{
FlowView fv = new FlowView()
{
DatetimeLastUpdate=DateTime.UtcNow.Ticks.ToString(),
Id=Guid.NewGuid().ToString(),
};
db.Flows.Add(fv);
db.SaveChanges();
}
更加详细的资料
1.SQLite CodeFirst
2.entity-framework-code-first-and-migration
源码下载 SQLite_EF6
最后修改:2017年8月25日12:11:13(1)