我使用
DatabaseSchemaUpdater实现了一些db版本逻辑,发现了一个问题.
如果我执行此代码
updater.DatabaseSchemaVersion = updater.DatabaseSchemaVersion + new Random().Next(10)+1; updater.Execute();
并通过后退按钮离开应用程序 – 一切正常.架构版本已修改.
如果我通过“开始”按钮离开然后再次启动应用程序(对于普通用户而言,通常情况下) – 没有任何更新. db有以前的版本.
调用Dispose()将解决这个问题,但是我们使用单个DataContext对象,因此这种方法对于这种情况会有点麻烦.
https://www.dropbox.com/s/wfyvwvjd12wifgl/DBUpdTest.zip – 测试项目 – 你可以简单地启动应用程序,并以不同的方式关闭,看看会发生什么(db ver写在文本框中).
解决方法
在过去的几个小时里我遇到了同样的问题,我搜索了很多Google.帮助我的唯一解决方案实际上是在数据库上下文中使用using关键字.之后,我重新创建了数据上下文.
// _dataContext is static. By checking for null I make sure that the database // creation and migration is only done once during the app lifecycle if (_dataContext == null) { // get the context using (_dataContext = new WorkTimesDataContext(IsoStoreDatabasefile)) { // do DB creation // do DB migration } // now that the context is disposed,recreate it _dataContext = new WorkTimesDataContext(IsoStoreDatabasefile); }
虽然官方文档明确指出:“当调用该方法(执行)时,所有更改都作为单个事务提交到本地数据库,包括版本更新,但似乎数据库在应用程序进入逻辑删除状态时未正确更新. “.