简体   繁体   English

EF 6 CodeFirst迁移不起作用

[英]EF 6 CodeFirst Migrations Not Working

Ok, so I've read the other threads and I'm still not getting anywhere. 好的,所以我已经阅读了其他主题并且我仍然没有得到任何结果。 I have my project set as Startup, I have a connection string in the App.config. 我将我的项目设置为Startup,我在App.config中有一个连接字符串。 I'm using LocalDb in VS2013. 我在VS2013中使用LocalDb。

If I delete my database, it creates it ok, BUT, if I try 如果我删除我的数据库,它会创建它,但是,如果我尝试

 PM> Enable-Migrations 

it tells me that Migrations have already been enabled in project TestCodeFirst. 它告诉我,项目TestCodeFirst中已经启用了迁移。 If I then try 如果我再尝试

 PM> Update-Database 

it tells me No migrations configuration type was found in the assembly 'TestCodeFirst', and suggests that I try to Enable-Migrations. 它告诉我在程序集“TestCodeFirst”中找不到迁移配置类型,并建议我尝试启用迁移。 I have tried adding the -Force parameter. 我试过添加-Force参数。 The Migrations folder has only Configuration.cs, and in that file, I've tried setting AutomaticMigrationsEnabled both true and false, with no difference. Migrations文件夹只有Configuration.cs,在该文件中,我尝试将AutomaticMigrationsEnabled设置为true和false,没有区别。 I'm not lost, cause I can always delete my DB and rerun, but I don't see any way to make the migrations feature work as advertised. 我没有丢失,因为我总是可以删除我的数据库并重新运行,但我没有看到任何方法使迁移功能像宣传的那样工作。 Confession: At one point, I took PM's suggestion and tried deleting the Migrations folder. 忏悔:有一次,我接受了PM的建议并试图删除Migrations文件夹。 That may have been a mistake. 这可能是一个错误。

I have this code snippet in my Program.cs file (I'm just testing this in a console app): 我在Program.cs文件中有这个代码片段(我只是在控制台应用程序中测试它):

class Program
{
    static void Main(string[] args)
    {
        using (TestContext db = new TestContext())
        {
            ...
            Db.ThisOrThat.Add(stuff);
            Db.SaveChanges();
            ...
        }
    }
}

The Entity Framework Code First approach creates your database automatically, a table will be automatically added to the database by the Code First that is helpful to record the schema of database synchronization with the related model class. Entity Framework Code First方法自动创建数据库,Code First将自动将表添加到数据库中,这有助于记录与相关模型类的数据库同步模式。 Entity Framework throws an error, the database is not synchronized with the class. 实体框架抛出错误,数据库与类不同步。

To perform the Code First Migration in the database: 要在数据库中执行代码优先迁移:

  • Delete the MDF file of the database 删除数据库的MDF文件

  • Open Package Manager Console and write the following command 打开包管理器控制台并编写以下命令

    Enable-Migrations -ContextTypeName ConsoleApp.Models.TestContext Enable-Migrations -ContextTypeName ConsoleApp.Models.TestContext

  • It creates the Configurations.cs class, then you can edit your code 它创建了Configurations.cs类,然后您可以编辑代码

  • After Building the application enter the following command in the Package Manager Console: 构建应用程序后,在程序包管理器控制台中输入以下命令:

    add-migration Initial

    update-database

Note: If you receive an error then you need to ensure that the mdf file is deleted permanently from the App_Data folder. 注意:如果收到错误,则需要确保从App_Data文件夹中永久删除mdf文件。 Otherwise you can go to the SQL Server Object Explorer, expand the localDb server and in the Database option ensure that the mdf file is deleted. 否则,您可以转到SQL Server对象资源管理器,展开localDb服务器,并在“数据库”选项中确保删除mdf文件。

  • Debug the appliction 调试应用程序

You'll need to run the "Add-Migration" command that will actually add the migration that the 'Update-Database' command will run: 您需要运行“Add-Migration”命令,该命令将实际添加“Update-Database”命令将运行的迁移:

Code First Migrations 代码优先迁移

"Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created" “基于自上次迁移创建以来对模型所做的更改,Add-Migration将支持下一次迁移”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM