如何在不丢失数据的情况下更新已部署的ASP MVC网站上的数据库模型 [英] How to update database model on a deployed asp mvc web site without losing data
问题描述
在部署后如何在asp mvc网站中更新数据库架构(模型),而又不会丢失任何先前的数据?
How to update database schema (model) in asp mvc website after it is deployed, without losing any previous data?
我刚刚将MVC5网站部署到了Azure网站.一切都很好,因此我开始上传一些数据.然后,我找出了需要更新的内容.很简单,我只想对其数据库架构进行一些更改.
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).
我首先使用实体框架6代码并启用了迁移:
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; }
}
收件人:
public class MyViewModel
{
public string Name {get;set; }
[AllowHtml] //ADDING THIS ONLY
public string Content { get; set; }
}
谢谢.
推荐答案
您可以在其他解决方案中使用此
you can, among other solution, use this
Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>());
其中 BlogContext
是您的上下文,而 Configuration
是配置代码(由 DbMigrationsConfiguration< T>
继承的配置类).
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).
另请阅读 http://msdn.microsoft.com/zh-CN/data/jj591621.aspx ,特别是最后几段.
Please also read http://msdn.microsoft.com/en-us/data/jj591621.aspx, specially the last paragraphs.
这篇关于如何在不丢失数据的情况下更新已部署的ASP MVC网站上的数据库模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!