Reputation: 235
How to update database schema (model) in asp mvc website after it is deployed, without losing any previous data?
I have just deployed an MVC5 website to azure web site. Everything is fine, thus I started to uploading some data. Then I figured out something that I needed to update. It is simple, I just want to make a slight change in its database schema.
In local machine (development stage), we can just run
Update-Database
on package manager console. And here is the question? how to do the same idea to the published version? I have not tried to re publish my solution, fearing that the data will be lost (the data is plenty, too much to re upload).
I am using entity framework 6 code first with migration enabled:
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
All I want to do is just adding an attribute to one of the data row:
public class MyViewModel
{
public string Name {get;set; }
public string Content { get; set; }
}
to:
public class MyViewModel
{
public string Name {get;set; }
[AllowHtml] //ADDING THIS ONLY
public string Content { get; set; }
}
Thanks.
Upvotes: 2
Views: 3041
Reputation: 7800
you can, among other solution, use this
Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>());
where BlogContext
is your context and Configuration
is the configuration class (the one descending from DbMigrationsConfiguration<T>
) generated by code first.
Of course you have to republish your application (at least binary part).
Please also read http://msdn.microsoft.com/en-us/data/jj591621.aspx, specially the last paragraphs.
Upvotes: 3
Reputation: 3779
Welcome to the scariness that is Code First Migrations in a production environment! One thing to note, if you enable automatic migrations, you can never revert the migration. Instead, it's recommended that you don't do automatic migrations. Instead, from your console, run add-migration "migrationName"
. This will add a migration to your migrations folder with an up and a down method, where you can see all the changes that are related to that migration. You can see what sql will be run against the database by running update-database -script. The changes will not actually be run, but you can see what sql is generated. Here is a good walkthrough on Code First Migrations and deployment and all the changes that are necessary in the web.config file.
I believe that allowing data loss is turned off by default when you use automatic migrations.
Upvotes: 0