EF6 Code First 系列 (四):SQLite的DropCreateDatabaseIfModelChanges和乐观并发控制
没什么好说的,能支持DropCreateDatabaseIfModelChanges和RowVersion的Sqlite谁都想要。EntityFramework7正在添加对Sqlite的支持,虽然EF7不知道猴年马月才能完成正式版,更不知道MySql等第三方提供程序会在什么时候跟进支持,但是EF7中的确出现了Sqlite的相关代码。Sqlite支持EF6的CodeFirst,只是不支持从实体生成数据库,估计有很多人因为这个原因放弃了使用它。现在SQLite.CodeFirst的简单实现可以让我们生成数据库,因此在等待EF7的可以预见的长时间等待中,我们可以使用SQLite.CodeFirst,毕竟我们只是开发的时候使用DropCreateDatabaseIfModelChanges,Release时不会使用更不用担心SQLite.CodeFirst的简单实现会带来什么问题。可以直接修改源码,也可以参考最后面通过反射实现自定义DropCreateDatabaseIfModelChanges的方式。
1.RowVersion的支持:
可以从我的上一篇:在MySql中使用和SqlServer一致的RowVersion并发控制中采用相同的策略即可。我已经测试过在Sqlite中的可行性。
1.首先是RowVersion的配置:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.AddFromAssembly(typeof(SqliteDbContext).Assembly);
modelBuilder.Properties()
.Where(o => o.Name == "RowVersion")
.Configure(o => o.IsConcurrencyToken()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None));
Database.SetInitializer(new SqliteDbInitializer(Database.Connection.ConnectionString, modelBuilder));
}
2.然后是SaveChanges的重写:
public override int SaveChanges()
{
this.ChangeTracker.DetectChanges();
var objectContext = ((IObjectContextAdapter)this).ObjectContext;
foreach (ObjectStateEntry entry in objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added))
{
var v = entry.Entity as IRowVersion;
if (v != null)
{
v.RowVersion = System.Text.Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());
}
}
return base.SaveChanges();
}
3.生成__MigrationHistory:
DropCreateDatabaseIfModelChanges则需要修改SQLite.CodeFirst的代码,SQLite.CodeFirst生成的数据库不包含__MigrationHistory信息,所以我们首先修改SqliteInitializerBase添加__MigrationHistory,__MigrationHistory表是通过HistoryRow实体的映射,我们直接在EF源代码中找到相关部分作为参考。修改SqliteInitializerBase的SqliteInitializerBase方法,配置HistoryRow实体的映射。
public const string DefaultTableName = "__MigrationHistory"; internal const int ContextKeyMaxLength = 300;
internal const int MigrationIdMaxLength = 150; protected SqliteInitializerBase(string connectionString, DbModelBuilder modelBuilder)
{
DatabaseFilePath = SqliteConnectionStringParser.GetDataSource(connectionString);
ModelBuilder = modelBuilder; // This convention will crash the SQLite Provider before "InitializeDatabase" gets called.
// See https://github.com/msallin/SQLiteCodeFirst/issues/7 for details.
modelBuilder.Conventions.Remove<TimestampAttributeConvention>(); modelBuilder.Entity<HistoryRow>().ToTable(DefaultTableName);
modelBuilder.Entity<HistoryRow>().HasKey(
h => new
{
h.MigrationId,
h.ContextKey
});
modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(MigrationIdMaxLength).IsRequired();
modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(ContextKeyMaxLength).IsRequired();
modelBuilder.Entity<HistoryRow>().Property(h => h.Model).IsRequired().IsMaxLength();
modelBuilder.Entity<HistoryRow>().Property(h => h.ProductVersion).HasMaxLength(32).IsRequired();
}
4.初始化__MigrationHistory:
继续修改InitializeDatabase方法,在创建数据库后,初始化__MigrationHistory的信息。虽然采用了HistoryRow,但初始化信息我们只简单的使用生成的SQL语句作为判定实体和配置是否改变的依据,因为后面的InitializeDatabase方法中也是我们自己来判定实体和配置是否改变。
public virtual void InitializeDatabase(TContext context)
{
var model = ModelBuilder.Build(context.Database.Connection); using (var transaction = context.Database.BeginTransaction())
{
try
{
var sqliteDatabaseCreator = new SqliteDatabaseCreator(context.Database, model);
sqliteDatabaseCreator.Create();
/*start*/
context.Set<HistoryRow>().Add(
new HistoryRow
{
MigrationId = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff"),
ContextKey = context.GetType().FullName,
Model = System.Text.Encoding.UTF8.GetBytes(sqliteDatabaseCreator.GetSql().ToCharArray()),
ProductVersion = "6.1.2"
});
/*end*/
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
} using (var transaction = context.Database.BeginTransaction())
{
try
{
Seed(context);
context.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
}
5.添加DropCreateDatabaseIfModelChanges支持
添加SqliteDropCreateDatabaseIfModelChanges类,在InitializeDatabase方法中判读实体和配置是否改变。需要注意的是,删除sqlite文件时,即使关闭Connection和调用GC.Collect()仍然在第一次无法删除文件,所以必须进行多次尝试。
public override void InitializeDatabase(TContext context)
{
bool dbExists = File.Exists(DatabaseFilePath);
if (dbExists)
{
var model = ModelBuilder.Build(context.Database.Connection);
var sqliteDatabaseCreator = new SqliteDatabaseCreator(context.Database, model);
var newSql = sqliteDatabaseCreator.GetSql();
var oldSql = "";
oldSql = System.Text.Encoding.UTF8.GetString(context.Set<System.Data.Entity.Migrations.History.HistoryRow>().AsNoTracking().FirstOrDefault().Model);
context.Database.Connection.Close();
GC.Collect();
if (oldSql == newSql)
{
return;
}
for (int i = 0; i < 10; i++)
{
try
{
File.Delete(DatabaseFilePath);
break;
}
catch (Exception)
{
System.Threading.Thread.Sleep(1);
}
}
} base.InitializeDatabase(context);
}
核心的代码已经贴出来,SQLite.CodeFirst本身的实现就比较简易,我添加的代码也比较简陋,因此在代码上没什么参考价值,只有使用和实用价值。毕竟只是在Debug开发时才需要这些功能的支持,对Sqlite本身和EF的提供程序没有任何影响。到这里终于松了口气,我们现在可以使用:Sql Server(CE)、Sqlite和Mysql进行Code First开发,采用相同的实体定义和配置,并且采用相同的并发控制。非Sql Server(CE)的并发控制和Sqlite不支持从代码生成数据库这两点终于克服了。
6.不修改源代码,使用反射实现
修改源码是由于SQLite.CodeFirst的内部类无法调用,考虑到可以使用反射,于是有了下面不需要修改源码,通过反射实现直接自定义DropCreateDatabaseIfModelChanges的方式:
class="code_img_closed" src="/Upload/Images/2015050113/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('894f68be-20a8-42c8-ac23-15dbc4525500',event)" src="/Upload/Images/2015050113/2B1B950FA3DF188F.gif" alt="" />
using SQLite.CodeFirst.Statement;
using System;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations.History;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.IO;
using System.Linq;
using System.Reflection; namespace SQLite.CodeFirst
{
public class SqliteDropCreateDatabaseIfModelChanges<TContext> : IDatabaseInitializer<TContext> where TContext : DbContext
{
protected readonly DbModelBuilder ModelBuilder;
protected readonly string DatabaseFilePath; public const string DefaultTableName = "__MigrationHistory";
private const string DataDirectoryToken = "|datadirectory|"; internal const int ContextKeyMaxLength = 300;
internal const int MigrationIdMaxLength = 150; public SqliteDropCreateDatabaseIfModelChanges(string connectionString, DbModelBuilder modelBuilder)
{
DatabaseFilePath = ConnectionStringParse(connectionString);
ModelBuilder = modelBuilder; // This convention will crash the SQLite Provider before "InitializeDatabase" gets called.
// See https://github.com/msallin/SQLiteCodeFirst/issues/7 for details.
modelBuilder.Conventions.Remove<TimestampAttributeConvention>();
ConfigMigrationHistory(modelBuilder);
} private string ConnectionStringParse(string connectionString)
{
var path = connectionString.Trim(' ', ';').Split(';').FirstOrDefault(o => o.StartsWith("data source", StringComparison.OrdinalIgnoreCase)).Split('=').Last().Trim();
if (!path.StartsWith("|datadirectory|", StringComparison.OrdinalIgnoreCase))
{
return path;
}
string fullPath; // find the replacement path
object rootFolderObject = AppDomain.CurrentDomain.GetData("DataDirectory");
string rootFolderPath = (rootFolderObject as string);
if (rootFolderObject != null && rootFolderPath == null)
{
throw new InvalidOperationException("The value stored in the AppDomains 'DataDirectory' variable has to be a string!");
}
if (string.IsNullOrEmpty(rootFolderPath))
{
rootFolderPath = AppDomain.CurrentDomain.BaseDirectory;
} // We don't know if rootFolderpath ends with '\', and we don't know if the given name starts with onw
int fileNamePosition = DataDirectoryToken.Length; // filename starts right after the '|datadirectory|' keyword
bool rootFolderEndsWith = (0 < rootFolderPath.Length) && rootFolderPath[rootFolderPath.Length - 1] == '\\';
bool fileNameStartsWith = (fileNamePosition < path.Length) && path[fileNamePosition] == '\\'; // replace |datadirectory| with root folder path
if (!rootFolderEndsWith && !fileNameStartsWith)
{
// need to insert '\'
fullPath = rootFolderPath + '\\' + path.Substring(fileNamePosition);
}
else if (rootFolderEndsWith && fileNameStartsWith)
{
// need to strip one out
fullPath = rootFolderPath + path.Substring(fileNamePosition + 1);
}
else
{
// simply concatenate the strings
fullPath = rootFolderPath + path.Substring(fileNamePosition);
}
return fullPath;
} private void ConfigMigrationHistory(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<HistoryRow>().ToTable(DefaultTableName);
modelBuilder.Entity<HistoryRow>().HasKey(
h => new
{
h.MigrationId,
h.ContextKey
});
modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(MigrationIdMaxLength).IsRequired();
modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(ContextKeyMaxLength).IsRequired();
modelBuilder.Entity<HistoryRow>().Property(h => h.Model).IsRequired().IsMaxLength();
modelBuilder.Entity<HistoryRow>().Property(h => h.ProductVersion).HasMaxLength(32).IsRequired();
} public string GetSql(DbModel model)
{
Assembly asm = Assembly.GetAssembly(typeof(SqliteInitializerBase<>));
Type builderType = asm.GetType("SQLite.CodeFirst.Builder.CreateDatabaseStatementBuilder"); ConstructorInfo builderConstructor = builderType.GetConstructor(new Type[] { typeof(EdmModel) });
Object builder = builderConstructor.Invoke(new Object[] { model.StoreModel }); MethodInfo method = builderType.GetMethod("BuildStatement", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public); var statement = (IStatement)method.Invoke(builder, new Object[] { });
string sql = statement.CreateStatement();
return sql;
} public void InitializeDatabase(TContext context)
{
var model = ModelBuilder.Build(context.Database.Connection);
var sqliteDatabaseCreator = new SqliteDatabaseCreator(context.Database, model);
var newSql = this.GetSql(model); bool dbExists = File.Exists(DatabaseFilePath);
if (dbExists)
{
var oldSql = System.Text.Encoding.UTF8.GetString(context.Set<System.Data.Entity.Migrations.History.HistoryRow>().AsNoTracking().FirstOrDefault().Model);
context.Database.Connection.Close();
GC.Collect();
if (oldSql == newSql)
{
return;
}
for (int i = 0; i < 10; i++)
{
try
{
File.Delete(DatabaseFilePath);
break;
}
catch (Exception)
{
System.Threading.Thread.Sleep(1);
}
}
}
using (var transaction = context.Database.BeginTransaction())
{
try
{
sqliteDatabaseCreator.Create();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
} using (var transaction = context.Database.BeginTransaction())
{
try
{
context.Set<HistoryRow>().Add(
new HistoryRow
{
MigrationId = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff"),
ContextKey = context.GetType().FullName,
Model = System.Text.Encoding.UTF8.GetBytes(newSql.ToCharArray()),
ProductVersion = "6.1.3"
});
Seed(context);
context.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
} protected virtual void Seed(TContext context)
{
}
}
}
希望你不是找了好久才找到这个解决方案。
EF6 Code First 系列 (四):SQLite的DropCreateDatabaseIfModelChanges和乐观并发控制的更多相关文章
- 用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署
用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署 这是微软官方SignalR 2.0教程Getting Started with En ...
- [转] 使用 MVC 5 的 EF6 Code First 入门 系列
译文:http://www.cnblogs.com/Bce-/category/573301.html 原文:http://www.asp.net/mvc/overview/getting-start ...
- MVC5中EF6 Code First启动慢及间隙变慢优化的实践经验(转)
最近项目在使用EF了,mvc使用EF确实方便,因为添加功能的时候可以使用vs自动生成用ef的增.删.查.改的模板,大的提高的工作效率.但是很多人都遇到过用EF开发的程序在第一次访问的时候会比用ADO纯 ...
- DocX开源WORD操作组件的学习系列四
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- Keil MDK STM32系列(四) 基于抽象外设库HAL的STM32F401开发
Keil MDK STM32系列 Keil MDK STM32系列(一) 基于标准外设库SPL的STM32F103开发 Keil MDK STM32系列(二) 基于标准外设库SPL的STM32F401 ...
- 前端构建大法 Gulp 系列 (四):gulp实战
前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gulp专家 前 ...
- 1.什么是Code First(EF Code First 系列)
EF4.1中开始支持Code First .这种方式在领域设计模式中非常有用.使用Code First模式,你可以专注于领域设计,根据需要,为你一个领域的对象创建类集合,而不是首先来设计数据库,然后来 ...
- Entityframework Code First 系列之数据注释
上一篇<Entityframework Code First 系列之项目搭建>讲了搭建一个Code First的控制台项目.里面有一些内容并没有扩展出来讲,因为篇幅有限.这篇针对上面内容中 ...
- Netty4.x中文教程系列(四) 对象传输
Netty4.x中文教程系列(四) 对象传输 我们在使用netty的过程中肯定会遇到传输对象的情况,Netty4通过ObjectEncoder和ObjectDecoder来支持. 首先我们定义一个U ...
随机推荐
- jquery练习题
1.自定义登录校验 用户输入用户名和密码 输入的用户名和密码不能为空 如果用户输入的用户名或者密码为空,你就提示它用户名不能为空或者密码不能为空. 知识点:文本操作相关 <!DOCTYPE ht ...
- 学习js的正确姿势
轻松入门: http://dwz.cn/6yYLoo 全面理解: http://www.shouce.ren/api/view/a/1116 大师进阶: http://es6.ruanyifeng.c ...
- java基础10 吃货联盟点餐系统
public class OrderMsg { public static void main(String[] args) throws Exception { /** * 订餐人姓名.选择菜品.送 ...
- spring 整合mybatis找不到${jdbc.driverClass}
1.检查是否设置了mapper扫描org.mybatis.spring.mapper.MapperScannerConfigurer类 在spring里使用org.mybatis.spring.map ...
- 访问HDFS报错:org.apache.hadoop.security.AccessControlException: Permission denied
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apac ...
- java中jdk安装配置信息
由于被人比较懒,有些做过的事情总是好忘,这个也不例外,索性就做个随笔. JAVA_HOMEC:\Program Files (x86)\Java\jdk1.7.0_03CLASSPATH.;%JAVA ...
- redis的ruby客户端(三)
1. 介绍 clients这里列出了redis所支持的语言的所有客户端程序,其中就有ruby的.有这么多的客户端,说明要实现redis的客户端是不难的.其实你只要掌握一种语言的socket编程就可以实 ...
- 浅析造成 JS 内存泄露的几种原因及解决方案
内存泄露是指一块被分配的内存既不能使用,又不能回收,直到浏览器进程结束.在C++中,因为是手动管理内存,内存泄露是经常出现的事情.而现在流行的C#和Java等语言采用了自动垃圾回收方法管理内存,正常使 ...
- (转载)Hibernate与Jpa的关系
我知道Jpa是一种规范,而Hibernate是它的一种实现.除了Hibernate,还有EclipseLink(曾经的toplink),OpenJPA等可供选择,所以使用Jpa的一个好处是,可以更换实 ...
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles
地址:http://codeforces.com/contest/764/problem/D 题目: D. Timofey and rectangles time limit per test 2 s ...