英文:
Question about migration techniques on a production db that seem confusing
问题
I use C#, .NET 和 EF Core/SQL for the data layer.
I've read a book on EF Core and one on TSQL and now I'm confused about some aspects of the migration techniques on a production system.
My questions are:
- I've read about the fact that you can just use
script migration -idempotent
to get a new/the applicable migration script, taking into account what migration was the last one in theEFMigration_History
table. In the book it says you NEED a tool such asDbUp
or RedGate'sflyaway
in order to run this script. Can't you just use MSSMS to do it? Or the VS SQL Server Explorer'sNew Query
? - There was also a technique, considered an easy one, where you would create a console app within your solution, this console app would just have
context.Database.Migrate
in it so that it could run whenever you call it and apply the new migration to a stopped app or a running app with some other tweaks, which is very confusing because maybe I'm not understanding this at all, but when the code is live you can't call/run a solution's project and have an impact on the production system right? And if so do you have an idea what this technique really is? - Do you recommend using SQL scripts to build migrations? The use of a SQL database comparison tool to produce migration form the current database schema to the desired database schema, it seems like you need good knowledge of SQL but I'm not too bad.
I'd like to know what you recommend to understand what's my best bet at not breaking my production system by planning ahead of time and making sure I understand what to do before I have to re do everything.
英文:
I use C#, .NET and EF Core/SQL for the data layer.
I've read a book on EF Core and one on TSQL and now I'm confused about some aspects of the migration techniques on a production system.
My questions are:
- I've read about the fact that you can just use
script migration -idempotent
to get a new/the applicable migration script, taking into account what migration was the last one in theEFMigration_History
table. In the book it says you NEED a tool such asDbUp
or RedGate'sflyaway
in order to run this script. Can't you just use MSSMS to do it ? Or the VS SQL Server Explorer'sNew Query
? - There was also a technique, considered an easy one, where you would create a console app within your solution, this console app would just have
context.Database.Migrate
in it so that it could run whenever you call it and apply the new migration to a stopped app or a running app with some other tweaks, which is very confusing because maybe I'm not understanding this at all, but when the code is live you can't call/run a solution's project and have an impact on the production system right ? And if so do you have an idea what this technique really is ? - Do you recommend using SQL scripts to build migrations ? The use of a SQL database comparison tool to produce migration form the current database schema to the desired database schema, it seems like you need good knowledge of SQL but I'm not too bad.
I'd like to know what you recommend to understand what's my best bet at not breaking my production system by planning ahead of time and making sure I understand what to do before I have to re do everything.
答案1
得分: 1
这是运行迁移的代码,它将输出成功和出错的迁移。你可以在此基础上进行扩展。
protected virtual void RunMigrations(String connectionString)
{
Console.WriteLine($"\n ---- 正在运行迁移 [{connectionString}]");
var dbContext = GetDbContext(connectionString);
var pendingMigrations = dbContext.Database.GetPendingMigrations();
var latestMigration = pendingMigrations.OrderByDescending(t => t).FirstOrDefault();
try
{
if (pendingMigrations.Any())
{
Console.WriteLine("运行迁移 \n - {0}", String.Join("\n - ", pendingMigrations));
dbContext.Database.Migrate();
Console.WriteLine("迁移完成...........................");
}
else
{
Console.WriteLine("没有待处理的迁移。");
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
var successfulMigrations = pendingMigrations.Intersect(dbContext.Database.GetAppliedMigrations());
var unsuccessfulMigrations = pendingMigrations.Except(successfulMigrations);
if (unsuccessfulMigrations.Any())
{
Console.WriteLine("无法应用以下迁移 \n - {0}", String.Join("\n -", unsuccessfulMigrations));
}
}
Console.WriteLine("--------------------------------------------------------------------------------");
}
英文:
Here is the code to run the migrations, which will output the Migrations run with success and errors. You should be able to build on top of it.
protected virtual void RunMigrations(String connectionString)
{
Console.WriteLine($"\n ---- Running migrations for [{connectionString}]");
var dbContext = GetDbContext(connectionString);
var pendingMigrations = dbContext.Database.GetPendingMigrations();
var latestMigration = pendingMigrations.OrderByDescending(t => t).FirstOrDefault();
try
{
if (pendingMigrations.Any())
{
Console.WriteLine("Running migrations \n - {0}", String.Join("\n - ", pendingMigrations));
dbContext.Database.Migrate();
Console.WriteLine("Migrations completed...........................");
}
else
{
Console.WriteLine("No pending migrations.");
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
var successfulMigrations = pendingMigrations.Intersect(dbContext.Database.GetAppliedMigrations());
var unsuccessfulMigrations = pendingMigrations.Except(successfulMigrations);
if (unsuccessfulMigrations.Any())
{
Console.WriteLine("Failed to apply the following migrations \n - {0}", String.Join("\n -", unsuccessfulMigrations));
}
}
Console.WriteLine("--------------------------------------------------------------------------------");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论