2
votes

After upgrading from EF 4.2 and Migration to EF 4.3 and enabling migration,restarting Visual studio and everything , whenever I try to call Update-Database/Add-Migration I get this:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException:
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments)
Update-Database : Exception has been thrown by the target of an invocation.
At line:1 char:1
+ update-database
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Exception has b... an invocation.:String) [Update-Database], RuntimeException
+ FullyQualifiedErrorId : Exception has been thrown by the target of an invocation.,Update-Database

I tried cleaning the whole project , deleting ef and migration and packages folder and doing it from the beginning and still same error !

Anyone facing the same error? Or have a solution for this?

2

2 Answers

5
votes

In case you have context and migration in separate projects, you need to use -StartupProjectName "YourProject" option of add-migration and update-database

The same error was in 4.3 beta. I encountered it in 4.3 release as well.

3
votes

Instead of using the Powershell commands, you can control the migration via code, and then tell it where your assembly and context are:

var configuration = new DbMigrationsConfiguration() {
  MigrationsAssembly = typeof(YourMigrations).Assembly,
  ContextType = typeof(YourContext)
};

Then you can either script it out or auto-run it by using the DbMigrator class:

var migrator = new DbMigrator(configuration);
var scripter = new MigratorScriptingDecorator(migrator);
string script = scripter.ScriptUpdate(null, null);