While working on a project using EF Code First recently, and came across the EF database migration. By following the Microsoft blog on how to perform the automatic migration, we hit an strange error when running the command “Update-Database”.
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)
It turns out we have this problem mainly because we have the DAL separated from the Web project, and the SQL connection is set in the Web project web.config
Our Solution:
Enable-Migrations -EnableAutomaticMigrations -ProjectName MySite.DAL
Update-Database -ProjectName MySite.DAL -StartUpProjectName MySite.Web -Verbose
MySite.Web has the connection string in web.config, and the DbContext is in the Mysite.DAL
See here for more information:http://stackoverflow.com/questions/9582129/exceptions-for-entity-framework-code-first-migrations