We have an ASP.NET MVC Core
app created with Code First
approach - using SQL Server 2012
. The app is already running in production where we need to change the length of a column from varchar(25)
to varchar(30)
. The approach we've used to make the above change is as follows. This approach seems long-winded. Question: Is there a better and faster way of doing it? We're using ASP.NET Core 1.1.1
with VS2017
Our Approach [so far]:
A. On Developer machine, change the corresponding Model as:
[Column(TypeName = "varchar(30)")]
public string ColumnName { get; set; }
B. Run PM> Add-Migration ...
and PM> Update-Database ...
commands in Package Manager
console of VS2017
C. Re-load the data to the corresponding table with changed column length
D. Re-deploy the database to production.
E. Re-deploy the project to production.
It "migration time" (actual/real time required by RDBS to execute DDL and update whole production database) is not very big (you agree to take your app offline for this time, and you don't expect "command timeout") than you may run migrations during app startup:
public void Configure(IApplicationBuilder app)
{
// your current app.UseXxx() calls
using (var tempScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var db = tempScope.ServiceProvider.GetService<YourDbContext>();
db.Database.Migrate();
}
}