CodeFirstMigrations更新数据库结构
作者:网络转载 发布时间:[ 2014/7/18 14:41:00 ] 推荐标签:数据开发 数据库
3:运行命令Enable-Migrations
可能会出现如下错误:
Checking if the context targets an existing database...
Detected database created with a database initializer. Scaffolded migration '201212090821166_InitialCreate' corresponding to existing database. To use an automatic migration instead, delete the Migrations folder and re-run Enable-Migrations specifying the -EnableAutomaticMigrations parameter.
Code First Migrations enabled for project MvcApplication1.
此时项目会出现如下文件夹:

打开configuation.cs,将作出如下修改:
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
再次执行Update-Database:
因为我把长度从max改为10,在更新数据结构时,它认为此操作会导致数据丢失,如下:
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending code-based migrations.
Applying automatic migration: 201212090848057_AutomaticMigration.
Automatic migration was not applied because it would result in data loss.
如果确保没事,只需给此命令加个强制执行的参数即可:
Enable-Migrations -Force
后再次执行:Update-Database

数据库中的原数据也没有丢失!

sales@spasvo.com