Entity Framework Code Migrations: Exception has been thrown by the target of an invocation

I get this error when I don't set up my connection strings properly.

Make sure the following are correct in your Web.config:

  1. Data Source
  2. Initial Catalog
  3. User Id
  4. Password

Had the same issue with Table-First update/generate from model. The solution was to replace the dot in the app.config connection string with the actual machine name of the SQL Server instance.


A bit late, but perhaps this is helpful to someone.

Several things you could check:

  • Make sure you are connecting to the right database: make sure you selected the right project in the console Default project drop-down list, or explicitly use the parameter -StartUpProjectName. This project must contain the right connection string in its settings. You can also explicitly set the -ConnectionString parameter.

  • If your solution has a startup project the console will use its connection string even if you stated a different project in the console Default project drop-down list or in the parameter -StartUpProjectName. Make sure you have the right startup project in the solution. Another way is having selected Multiple startup projects in you solution configuration options. In that case the console will accept the values you provided instead of looking for the connection string in a solution startup project.

  • Perhaps your current database is not synchronised with your current last migration (the one previous to the new one you are trying to generate). Try running Get-Migrations to see which is the current migration applied to your database. If it is not the latest one, run Update-Database using the parameters -SourceMigration and -TargetMigration to update it to the latest migration.