Entity Framework Code First Migrations TargetInvocationException Fix

Today, when I tried to add a new migration or update my database to the existing migration (Update-Database) I kept getting the following error:

PM> Add-Migration BlogCreated

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) Exception has been thrown by the target of an invocation.

This occurred because I separated out my context and migration files out into different projects. This isn’t wrong per se, but it does confuse Code First Migrations. To resolve this issue, I had to specify what project my migrations actually exist in.

PM> Add-Migration BlogCreated -StartupProjectName "Blog.Infrastructure"

If you are getting this error, specify your StartupProjectName and you should be good to go.

Creative Commons License

What do you think?