实体框架代码第一个迁移类文件 [英] Entity Framework Code First Migration Class File

查看:320
本文介绍了实体框架代码第一个迁移类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC 4与Entity Framework 5.0构建应用程序。我最近了解了可以在您的数据库的应用程序上实现的EF Code-First Migrations工具。我在PM控制台中运行了Enable-Migration,并成功启用了InitialCreate.cs类,并在表创建中填充了适当的up()和down()方法。



然后我继续添加了另一个类(Category.cs),它有两个简单的属性(id,name),并希望将这个迁移添加到数据库中。我在我的PM中运行Add-Migration AddCategoryClass以获取此消息:

 脚手架迁移AddCategoryClass。 
此迁移文件的设计器代码包含当前代码的快照
第一个模型。此快照用于在构建下一个迁移时计算模型的更改。如果您对模型进行了要包含在此迁移中的其他更改,则可以通过再次运行Add-Migration 201307301938256_AddCategoryClass来重新构建它。

它在Migrations文件夹中创建了迁移文件,但是当我打开.cs文件它创建,Up()和Down()方法是空白的。我期望它创建一个表,并分别添加字段。出于好奇,我运行更新数据库,并成功地通过(在Configuration.cs文件中更改AutomaticMigration = true后)。当我建立我的应用程序,表出现在数据库中。



所以我的问题是 - >为什么是Up p>

对于记录,如果Configuration.cs文件中的AutomaicMigrations = false,则Update-Database命令不起作用;给我以下错误:

 无法更新数据库以匹配当前模型,因为有待处理的更改,并禁用自动迁移。将挂起的模型更改写入基于代码的迁移或启用自动迁移。将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移。 

我假设这些不应该,并且它将有助于在未来看看我的迁移,看看我做了什么。任何帮助或指导是伟大的。



我在Win 7 64位使用VS 2012。

解决方案

这可能是因为您有一个数据库初始化程序,始终保持数据库与模型同步,而不管迁移。



__ MigrationHistory ,您可能会看到架构已通过 MigrateDatabaseToLatestVersion 初始化 MigrationId 将类似于 201307290913337_AutomaticMigration



要修复它,删除数据库,注释掉对模型的更改,运行 Update-Database - 现在在更改中注释,然后再次添加迁移。如果 __ MigrationHistory 表的最后一个记录确实是 xxxxxxx_AutomaticMigration



一个,你可以删除它,然后再次运行 Add-Migration ,你的迁移类应该在它的 Up Down 方法。



您应该确保运行 Add-Migration


I'm building an application using ASP.NET MVC 4 with Entity Framework 5.0. I've recently learned about EF Code-First Migrations tool you can implement on your application for your database. I ran "Enable-Migration" in the PM Console, and it enabled successfully and created the "InitialCreate.cs" class as expected with the appropriate up() and down() methods filled in with the table creations.

Then I went ahead and added another class (Category.cs) with 2 simple properties (id, name) and wanted to add this migration to the database. I ran "Add-Migration AddCategoryClass" in my PM to get this message:

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.

It created the migration file in the Migrations folder, but when I open up the ".cs" file it created, the Up() and Down() methods are blank. I was expecting it to create a table and add the fields respectively. Out of curiousity, i ran "Update-Database" and it went through successfully (after changing AutomaticMigration = true in the Configuration.cs file). When I built my application, the table appeared in the database.

So my question is --> Why are the Up() and Down() methods blank?

For the record, the "Update-Database" command does not work if AutomaicMigrations = false in the Configuration.cs file; giving me the following error:

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.

I'm assuming these shouldn't be and it'd be helpful in the future to look at my migrations and see what I did. Any help or guidance would be great. Thank you in advanced.

I'm using VS 2012 on Win 7 64bit.

解决方案

This is likely because you have a database initializer, that always keep the database in sync with the model, regardless of migrations.

Take a look at the __MigrationHistory and you'll likely see that the schema was updated by the MigrateDatabaseToLatestVersion initializer. The MigrationId will be something like 201307290913337_AutomaticMigration.

To fix it, delete the database, comment out the changes to your model, run Update-Database - now comment in the changes, and add the migration again.

If the last record of the __MigrationHistory table is indeed a xxxxxxx_AutomaticMigration one, you can just delete it and run Add-Migration again, and your migration class should have something useful in its Up and Down methods.

You should make sure that you run Add-Migration when the changes are not already made.

这篇关于实体框架代码第一个迁移类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆