This content originally appeared on DEV Community and was authored by Amir Hossein Shekari
As a .NET developer, I had a close call when I applied a migration to my production database without changing the database connection string. I was mortified when I realized my mistake, but I quickly got to work finding a solution to prevent it from happening again.
That's when I came up with the idea to add a console message that displays the migration details before applying it. This simple addition gives me the chance to review the migration details and ensure that I am working with the correct database before making any changes.
With this new safeguard in place, I can breathe easy knowing that my production database is safe from accidental alterations. It just goes to show that sometimes the simplest solutions can be the most effective.
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
// ... Some code to generate dbContextBuilder
var context = new ApplicationDbContext(dbContextBuilder.Options);
// This is where magic happends
var pendingMigrations = context.Database.GetPendingMigrations();
Console.WriteLine("*********************************************\n");
Console.WriteLine("This command is going to apply migrations with following details");
Console.Write("ConnectionString: ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(connectionString);
Console.ResetColor();
Console.Write("Migrations:\n\t");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(string.Join("\n\t", value: pendingMigrations.ToArray()));
Console.ResetColor();
Console.WriteLine();
Console.WriteLine("*********************************************");
Console.WriteLine("Do You confirm? (Y/N)");
var userInput = (Console.ReadLine());
if (userInput is "Y" or "y")
return context;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Aborted!");
Environment.Exit(1);
return null;
}
}
- More info about the
ApplicationDbContextFactory
This content originally appeared on DEV Community and was authored by Amir Hossein Shekari

Amir Hossein Shekari | Sciencx (2023-04-25T15:03:58+00:00) How I Saved My Production Database with One Simple Console Message. Retrieved from https://www.scien.cx/2023/04/25/how-i-saved-my-production-database-with-one-simple-console-message/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.