在应用自动迁移之前询问
Ask before applying automatic migration
如果我的数据库版本过时,我希望我的演示 win-forms 应用程序询问用户是否应用自动迁移。
我从MSDN看到自动迁移是由
控制的
AutomaticMigrationsEnabled = true // or false
在迁移配置中 class。
所以我可以在这一点上放一个消息框,但是我只想问一下版本是否过时了。
我可以查询
context.Database.CompatibleWithModel(true)
了解模型是否需要升级,但是我不知道如何从配置初始值设定项中访问上下文
我使用错误处理来确定是否要求 运行 数据库更新。
在我的上下文中,我把
public class MyAppDbContext : DbContext
{
public MyAppDbContext(string connectionString)
: base(connectionString)
{
if (!Database.Exists())
{
Database.SetInitializer(new MyAppDbInitializerCreateIfNotExists());
}
else
{
if (!Database.CompatibleWithModel(true))
{
throw new InvalidOperationException("The database is not compatible with the entity model.");
}
}
}
public MyAppDbContext() // used for migrations
: base("name=ApplicationDatabase")
{
Database.SetInitializer(new MyAppDbInitializerCreateIfNotExists());
}
public MyAppDbContext(bool forceMigration) // used for migrations
: base("name=ApplicationDatabase")
{
if (forceMigration)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyAppDbContext, Configuration>());
}
}
public bool SanityCheck()
{
try
{
var rec = JobLists.Select(x => x.Id == 1);
return true;
}
catch (Exception)
{
return false;
}
}
// the rest of the class
}
在我的程序中我输入了
static class Program {
[STAThread]
static void Main() {
/// some set up code missing here
MyAppWindowsFormsApplication winApplication = new MyAppWindowsFormsApplication();
try
{
winApplication.Setup();
winApplication.Start();
}
catch (Exception e)
{
if (e.InnerException.Message == "The database is not compatible with the entity model.")
{
var msg = new StringBuilder();
msg.AppendLine("The database structure has changed.");
msg.AppendLine("Do you want to upgrade the database structure. (Say no and call support if you are unsure)");
if (MessageBox.Show(msg.ToString(), "Upgrade database", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
var o = new MyAppDbContext(true);
o.SanityCheck();
MessageBox.Show("Database has been upgraded");
}
}
else
{
winApplication.HandleException(e);
}
}
}
如果我的数据库版本过时,我希望我的演示 win-forms 应用程序询问用户是否应用自动迁移。
我从MSDN看到自动迁移是由
控制的AutomaticMigrationsEnabled = true // or false
在迁移配置中 class。
所以我可以在这一点上放一个消息框,但是我只想问一下版本是否过时了。
我可以查询
context.Database.CompatibleWithModel(true)
了解模型是否需要升级,但是我不知道如何从配置初始值设定项中访问上下文
我使用错误处理来确定是否要求 运行 数据库更新。
在我的上下文中,我把
public class MyAppDbContext : DbContext
{
public MyAppDbContext(string connectionString)
: base(connectionString)
{
if (!Database.Exists())
{
Database.SetInitializer(new MyAppDbInitializerCreateIfNotExists());
}
else
{
if (!Database.CompatibleWithModel(true))
{
throw new InvalidOperationException("The database is not compatible with the entity model.");
}
}
}
public MyAppDbContext() // used for migrations
: base("name=ApplicationDatabase")
{
Database.SetInitializer(new MyAppDbInitializerCreateIfNotExists());
}
public MyAppDbContext(bool forceMigration) // used for migrations
: base("name=ApplicationDatabase")
{
if (forceMigration)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyAppDbContext, Configuration>());
}
}
public bool SanityCheck()
{
try
{
var rec = JobLists.Select(x => x.Id == 1);
return true;
}
catch (Exception)
{
return false;
}
}
// the rest of the class
}
在我的程序中我输入了
static class Program {
[STAThread]
static void Main() {
/// some set up code missing here
MyAppWindowsFormsApplication winApplication = new MyAppWindowsFormsApplication();
try
{
winApplication.Setup();
winApplication.Start();
}
catch (Exception e)
{
if (e.InnerException.Message == "The database is not compatible with the entity model.")
{
var msg = new StringBuilder();
msg.AppendLine("The database structure has changed.");
msg.AppendLine("Do you want to upgrade the database structure. (Say no and call support if you are unsure)");
if (MessageBox.Show(msg.ToString(), "Upgrade database", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
var o = new MyAppDbContext(true);
o.SanityCheck();
MessageBox.Show("Database has been upgraded");
}
}
else
{
winApplication.HandleException(e);
}
}
}