Monday, December 31, 2018

migrations in Entity Framework 6

You need to run a command as follows at PowerShell and honestly I'd just run it at the NuGet command prompt inside Visual Studio:

EntityFramework\Add-Migration "AddReportOrderAndSurveyOrderToProgramSurvey"
      -ConfigurationTypeName "MyConfiguration"

 
 

There probably is a way to spec which project in a Visual Studio solution this applies to. On the team I am currently on, we sort of hack around that by just setting the infrastructure project to be the startup project just for a moment. We also change the "Default project:" dropdown at the NuGet console. Another thing you'll need is some sort of change to one of Entity Framework's code first files. In my case I added two nullable int getsetters to the ProgramSurvey POCO and the migration made a file called 201812311747128_AddReportOrderAndSurveyOrderToProgramSurvey.cs in the "Migrations" folder in the infrastructure project. This file also had companion Designer.cs and .resx files, but the .cs file itself looked like so:

namespace Something.Business.Migrations
{
   using System;
   using System.Data.Entity.Migrations;
   
   public partial class AddReportOrderAndSurveyOrderToProgramSurvey : DbMigration
   {
      public override void Up()
      {
         AddColumn("dbo.ProgramSurveys", "ReportOrder", c => c.Int());
         AddColumn("dbo.ProgramSurveys", "SurveyOrder", c => c.Int());
      }
      
      public override void Down()
      {
         DropColumn("dbo.ProgramSurveys", "SurveyOrder");
         DropColumn("dbo.ProgramSurveys", "ReportOrder");
      }
   }
}

 
 

By the way, if you every screw up one of these migrations and you decide that you just want to revert code and drop and recreate the database, you will find that Entity Framework can still get lost somehow. You may end up with a second migration that tries to address something you just threw away. I got around as much by closing Visual Studio 2017, deleting what was in the bin folder from the infrastructure project, opening Visual Studio anew, rebuilding the solution, and then starting to set everything else up from the beginning.

No comments:

Post a Comment