EF Code First: Need to update column length and data on production server

asp.net-core ef-migrations entity-framework-core

Question

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.

1
2
9/25/2017 6:19:53 PM

Popular Answer

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();
    }
}
0
9/27/2017 12:34:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow