EF Migrations migrate.exe generate script - entity-framework

I'm playing around with Entity framework and continuous builds. So far i'm able to run a migration or series of migrations without any problem by using migrate.exe and the appropriate arguments.
However, i've hit trouble when trying to get migrate.exe to kick out a script, rather than perform the migration, in the same way as I could get by running
update-database -TargetMigration TestMigration -script
from within Package Manager Console in Visual Studio.
Is there currently a way to do this?
Thanks.

Since the 10/22/2017 you can do it thanks to this PR:
https://github.com/aspnet/EntityFramework6/commit/02ec6b8c9279f93f80eeed1234e5ce0acfce5f31
Here the Entity Framework 6.2 release notes that implements this functionality (see 'Migrate.exe should support -script option' section):
https://blogs.msdn.microsoft.com/dotnet/2017/10/26/entity-framework-6-2-runtime-released/
Follow those steps:
Copy the file migrate.exe from the '\packages\EntityFramework.6.2.0\tools' to the target 'bin' folder (for example on the production server) after that you deployed the new assembly that contains the new migrations
Open the command line in the folder and launch this command:
migrate.exe yourMigrationAssembly.dll
/startupConfigurationFile=”..\web.config”
/scriptFile="migrationOutput.sql"
It will generate the the file "migrationOutput.sql" that contains the SQL you have to execute on your target environment DB based on the migrations that are not yet applied on it.

It is currently not supported. Please add your vote to the issue: Migrations: -Script for Migrate.exe

I encountered the same problem and indeed the option is available in the package manager console in Visual Studio. So I opened up the powershell script and the entity framework dll and built a small executable so you can generate the scripts from command line.The source code is available as-is and without any warranty here;
EF6
EF5

You can write a simple c# console application or use something like Linqpad to generate the script using the Entity Framework Infrastructure objects. You will just need to load the DLL with your DbMigrationsConfiguration class and instantiate it. Here is the code similar to what is working for me:
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.Infrastructure;
const string ScriptFile = "Migration.sql";
const string ConnectionString = #"Server=.\SqlExpress;Database=...;Trusted_Connection=True;";
const bool AutomaticMigrationDataLossAllowed = false;
var targetDb = new DbConnectionInfo(ConnectionString, "System.Data.SqlClient");
var config = new MyDbMigrationsConfiguration
{
AutomaticMigrationDataLossAllowed = AutomaticMigrationDataLossAllowed,
TargetDatabase = targetDb,
};
var migrator = new DbMigrator(config);
var scripter = new MigratorScriptingDecorator(migrator);
var script = scripter.ScriptUpdate(null, null);
File.WriteAllText(ScriptFile, script);
Console.WriteLine("Migration Script Generated: " + ScriptFile);

Related

How to Pass CreateDbContext Args Parameters?

I have a multiple DBContexts and DesignTimeDbContextFactory implementation like below.
public class MasterDbContextFactory : IDesignTimeDbContextFactory<MasterDbContext>
{
public MasterDbContext CreateDbContext(string[] args)
{
StringBuilder sb = new StringBuilder();
foreach (var arg in args)
sb.AppendLine(arg);
System.IO.File.WriteAllText("D:\\debug.txt", sb.ToString());
string server = args[1];
string databaseName = args[2];
string connectionString = string.Format(Constant.Connection, server, databaseName);
var optionsBuilder = new DbContextOptionsBuilder<MasterDbContext>();
optionsBuilder.UseSqlServer("connection string");
return new MasterDbContext(optionsBuilder.Options);
}
}
As mentioned here, I apply like this.
Update-Database -Context MasterDbContext -AppArgs 'LAPTOP-E4UBP70J' 'MASTER_USER'
But I don't see all args parameters.
The feature is only available in recent release EF Core 5.0 Preview 6
Flow arguments into IDesignTimeDbContextFactory
Arguments are now flowed from the command line into the CreateDbContext method of IDesignTimeDbContextFactory. For example, to indicate this is a dev build, a custom argument (e.g. dev) can be passed on the command line:
dotnet ef migrations add two --verbose --dev
This work well at 5.0.0-preview.7.20365.15 version.
Adding migration with additional args:
Add-Migration InitialCreate -Context MasterDbContext -OutputDir Migrations\Master -Args 'P1 P2'
Updating migration with additional args:
Update-Database -Context MasterDbContext -Args 'P1 P2'
Here is source
Using Entity Framework Core .NET Command-line Tools version 5.0.7 or newer you can pass custom 'application arguments' into the CreateDbContext method by appending -- to the end of the command, followed by a space and then your arguments.
e.g. dotnet ef database update --context MyContext -- MyFirstArg MySecondArg
See
https://github.com/dotnet/efcore/issues/8332#issuecomment-644918706 which describes how to pass arguments when using both Command-Line and Powershell commands.
Command-Line
The commands will no longer interpret any extraneous arguments as application arguments. Instead, if you want to call a dotnet ef command with application arguments, you must now put -- after you pass in all the normal arguments, followed by whatever you want to the application arguments to be. All arguments after the -- will be interpreted as application arguments. If you need to pass in an application argument which, for instance, contains a space you will need to quote that argument e.g.
dotnet ef migrations add InitialCreate -- FirstAppArg "This is all the second application argument" ThirdAppArg

ConfigurationType error when using Entity Framework migrate.exe with multiple migration configurations

In my solution, I have a Data project that contains multiple Entity Framework 6.1.3 migration configuration classes. My goal is to run Entity Framework migration steps - for one of them, against an existing database - from TeamCity (or, to simplify, from a command line).
The migration configuration class I am using is the following:
namespace MyProject.Data
{
public partial class MyCustomMigrationConfiguration :
DbMigrationsConfiguration<MyCustomContext>
{
public MyCustomMigrationConfiguration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = true;
MigrationsDirectory = #"Migrations\MyCustomContext\MigrationSteps";
}
}
}
I can successfully run the following command from Package Manager Console in Visual Studio:
Update-Database -Verbose -StartUpProject Web -ConnectionString '-my
connection string here-' -ConfigurationTypeName
MyCustomMigrationConfiguration -ConnectionProviderName
'System.Data.SqlClient'
I want to do the same thing from a command line, so I run this:
migrate.exe MyProject.Data.dll "MyCustomMigrationConfiguration"
/startUpConfigurationFile=MyProject.Web.dll.config
/connectionString="-my connection string here-;"
/connectionProviderName="System.Data.SqlClient" /verbose
However, I get the following error:
ERROR: The migrations configuration type
MyCustomMigrationConfiguration was not be found in the assembly
‘MyProject.Data'.
Any suggestions on how to fix this, please?
You can specify the directory where are all the dependencies (assemblies) needed to run your code. You can do that by using the /startUpDirectory option, as explained here:
Specify working directory
Migrate.exe MyApp.exe /startupConfigurationFile=”MyApp.exe.config” /startupDirectory=”c:\MyApp”
If you assembly has dependencies or reads files relative to the working directory then you will need to set startupDirectory.
Found the solution (I ended up downloading the Entity Framework source code from http://entityframework.codeplex.com/ and debugging the migrate console application).
Apparently, all the dependencies of MyProject.Data.dll need to be copied in the same folder with it and migrate.exe, otherwise the Entity Framework migrate.exe tool will throw the misleading error message above.
Entity Framework could really use better error handling and a clearer error message in this case.
As a reference to Entity Framework devs: the following code in TypeFinder.cs was returning a null type because the dependencies of MyProject.Data.dll were not copied in the folder of migrate.exe:
type = _assembly.GetType(typeName, false);

how to configure doctrine command line tools on zenframework 2

i am using doctrine 2 on zendframework 2. i have configured both correcly and they are both working.
i however wish to use doctrine's command line tool to generate entities etc.
i have followed doctrine's instructions and created a cli-config.php page in the root of my application:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html
i am however lost on two issues;
the configuration requires a bootstrap php page; however, zendframework 2 does not use a bootstrap page; so what would the equivalent be?
Secondly, it requires us to obtain an entity mangager; would the method below be the correct way to get the entity manager:
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
return $this->em;
}
below is how the cli-config.php page should look;
// cli-config.php
require_once 'my_bootstrap.php';
// Any way to access the EntityManager from your application
$em = GetMyEntityManager();
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
i would really appreciate any help or advice on this matter.
warm regards
Andreea
the matter has been resolved:!!
it did not work because i was using a cygdrive command line. however, when i switched to git bash it worked perfectly. with git bash i have to use the command:
C: > cd project-directory
project-dir > vendor\bin\doctrine-module orm:validate-schema
If you have started your project using the Zend Skeleton Application you do have a composer.json file. You just need to include the DoctrineORMModule (instructions here)
Then, using the CMD just type
C: > cd project-directory
project-dir > vendor\bin\doctrine-module orm:validate-schema
There you go.
Once you have set up doctrine2 and zf2, you should be able to simply run all CLI commands.
php public/index.php orm:generate-entities
Using the parameters as described in the official documentation.
Note: DoctrineModule and DoctrineORMModule need to be enabled within your application.config.php
You need to install the doctrine/doctrine-orm-module with your Composer dependency manager. To do that, cd to your web site's top-level directory and type the following command:
php composer.phar require doctrine/doctrine-orm-module *
After executing this command, the DoctrineModule and DoctrineOrmModule will be installed into your vendor folder, and Doctrine commands will become available.
For more information about DoctrineOrmModule, see this:
https://github.com/doctrine/DoctrineORMModule

Entity Framework: Add-Migration fails with Unable to update database

I have been using Entity Framework (5.0) for a while now in a project (ASP.NET MVC in VS2012 Express). Right now, though, I am no longer able to add migrations.
PM > Add-Migration -projectName MyProject.DAL TestMigration
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 do not know if this gives any clue but the 'Unable to ..." text is displayed in red.
I have tried to enable automatic migration (which makes no sense as I am trying to write the pending model changes to a code-based migration) and that results in the required migration in the database. However this is not what I want because I then I do not have a migration in the project.
I have tried to remove the database and recreate the database. The database is recreated (up to the previous migration) but when I then try to use the Add-Migration I still get the "Unable to update.." error.
Edit
I tried the -force parameter but with no difference.
The contents of my configuration class (I did not change anything after the previous migration):
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(Bekosense.DAL.Context.BekosenseContext context)
{
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageDrop);
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageCreate);
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentDrop);
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentCreate);
context.Database.ExecuteSqlCommand(Properties.Resources.AddDbUsers);
}
Edit 2
I found out that I am able to do an add-migration when I comment the following line out in my DbContext:
//Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
when I leave the line above active and comment out everything in the Configuration file, it still won't work.
Why is the Database.SetInitializer line causing this strange behaviour?
You can reset the entity framework to solve your problem [But keep it mind it will bring the Migration to the default state]
Note: To take a backup before performing the following
You need to delete the present state:
Delete the migrations folder in your project
Delete the __MigrationHistory table in your database (may be under system tables)
You will find the __MigrationHistory table in your database [Under App_Data Folder]
Then run the following command in the Package Manager Console:
Enable-Migrations -EnableAutomaticMigrations -Force
Use with or without -EnableAutomaticMigrations
And finally, you can run:
Add-Migration Initial
This may also help you
Never use automigrations, that gave me problems in the past (when migrating the database down, use the correct way to do it from the start!)
This line should be in your global.asax:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
And not in your DbContext!
Other tips if the above won't help:
Perhaps you have multiple layers in your application:
Add-Migration 000 -StartupProjectName "NameOfTheProjectInSolutionExplorer" -ConfigurationTypeName "MyConfiguration" -ConnectionString "theconnectionstring;" -ConnectionProviderName "System.Data.SqlClient" -Verbose
Above is the Add-Migration command i use for a multi-layered application.
Same thing for an update of the database
Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -script
In my case I've got the same error because I was forcing ObjectContext.CommandTimeout on class DbContext at constructor method during migration.
Try removing it
((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 5000;
This worked for me:
update-database -targetmigration:"0" -force -verbose
add-migration Initial
update-database

Strategy for Getting SQL for AutomaticMigrations

We're using EF 5RC, code first with migrations. I feel like this should be an easy question to answer (i hope). Is there a good way to figure out what the automatic migration is attempting to do.
I've added a migration through the Add-Migration PS command.
I've invoked Update-Database and all seems fine with that migration.
Now - I'm just running Update-Database like i normally do, but with the following error:
PM> update-database -Verbose
Using StartUp project 'Web'.
Using NuGet project 'DataAccess'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'UserGroup' (DataSource: (localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Configuration).
No pending code-based migrations.
Applying automatic migration: 201206301526422_AutomaticMigration.
Automatic migration was not applied because it would result in data loss.
Notice, i'm adding the -Verbose option, and I've tried it again with the -Script. But I have no idea what we're migrating to; and what SQL - or what it thinks will result in data loss.
I do not want to simply enable "allow data loss" here, but am trying to understand how to troubleshoot these migrations.
Thank you in advance!
Just run:
PM> Update-Database -Script -Force
This will generate the SQL and display it in a window without running it.
I got this error on Azure after a publish , but there u cant use -Force , so global solution (and no need for -Force on local too )
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true; // <-- THIS LINE
}