5

Wondering if anyone can help me solve this.

I am able to use the Package Manager Console to Enable-Migrations, Add-Migrations etc etc. But I want to be able to do this using c# code via the PowerShell object

What I have so far is:

var ps = PowerShell.Create();
ps.AddCommand("Set-ExecutionPolicy");
ps.AddArgument("Unrestricted");
ps.AddCommand("Import-Module");
ps.AddParameter("Name");
ps.AddArgument(@"'.\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1'";
ps.AddCommand("Add-Migration");
ps.AddParameter("Name");
ps.AddArgument("Migration21Sept");
ps.AddParameter("ProjectName");
ps.AddArgument("Context1");
ps.Invoke();

But when I execute this code I get an error stating: "The term 'Add-Migration' is not recognized as the name of a cmdlet, function, script file, or operable program"

mick_a_p
  • 91
  • 1
  • 5
  • Means that the module (`EntityFramework.psm1`) wasn't loaded correctly – Mathias R. Jessen Sep 21 '15 at 11:17
  • you can also use [DbMigrator](https://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrator%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396) to migrate a database. What is your actual problem? It seems to me that this is an XY-problem – default Sep 21 '15 at 11:17
  • Thanks for you reply.. Would there be any reason for it not loading properly? Or could you point me in a direction of doing it correctly? – mick_a_p Sep 21 '15 at 11:18
  • 1
    @Default I know about the Migrator, and the ToolingFacade class. I can scaffold a new migration through the ToolingFacade class, and within this scaffold object is the code that is needed to create this migration. So, in theory, I could create a new file and add the code to it. Then update this migration. Although a problem I was having was it was not noticing my model changes, and the Up&down methods were empty. I just need a way, by code, to create a new migration file. So i will use the DbMigrator to actually handle it. – mick_a_p Sep 21 '15 at 11:23

1 Answers1

-2

Browse to your entity directory, shift + right click then select "Open powershell window here"

dotnet ef migrations add "MigrationName"

Ben
  • 1
  • 1