我正在构建一个使用ASP.NET MVC 4和Entity Framework 5.0的应用程序。我最近了解了您可以在您的数据库应用程序上实现的EF Code-First Migrations工具。我在PM控制台中运行了“Enable-Migration”,并且它成功启用并按照预期创建了“InitialCreate.cs”类,并在表创建中填充了适当的up()和down()方法。实体框架代码第一个迁移类文件
然后我继续添加另一个具有2个简单属性(id,name)的类(Category.cs),并希望将此迁移添加到数据库。我跑“添加迁移AddCategoryClass”我下午来得到这个消息:
Scaffolding migration 'AddCategoryClass'.
The Designer Code for this migration file includes a snapshot of your current Code
First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration 201307301938256_AddCategoryClass' again.
它创造了迁移文件夹迁移文件,但是当我打开了“的.cs”文件,它创建的,向上( )和Down()方法是空白的。我期待它创建一个表并分别添加字段。出于好奇心,我运行了“Update-Database”,并成功完成了(在Configuration.cs文件中更改AutomaticMigration = true后)。当我建立我的应用程序时,表格出现在数据库中。
所以我的问题是 - >为什么Up()和Down()方法是空白的?
对于记录,如果Configuration.cs文件中AutomaicMigrations = false,则“Update-Database”命令不起作用;给我以下错误:
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
我假设这些不应该是,它会是有益的,今后看我的迁移,看看我做了什么。任何帮助或指导都会很棒。先进的谢谢你。
我在Win 7 64bit上使用VS 2012。
你有一个或两个迁移文件(configuration.cs除外)吗? – SOfanatic
现在它显示了两个,InitialCreate和AddCategoryClass。但第二个在你的配置类中有空白方法 – Sixers17
是'AutomaticMigrationsEnabled = true'? –