I created an MVC 5 application in VS 2013 Professional and then used EF 6.1 code first with an existing DB on SQL Server Express. When I try to create the views I’m using the “New scaffolded item…” then selecting the “MVC 5 controller with views, using Entity Framework.” I select the model and context classes and click OK. Then the following error message appears and no code is created. I’ve uninstalled EF Power Tools with the same error.
Error
There was an error running the selected code generator: ‘Exception has been thrown by the target of an invocation.’
I've also tried uninstalling/reinstalling VS 2013 and SQL Server with no changes.
Any other ideas about what might cause this error?
Check relationships between entities or other model design problem. For testing, create new class model without no relationship and use Scaffold to generate controllers and views. Works for me.
If by any chance you are following "Getting Started with Entity Framework 6 Code First using MVC 5" by Tom Dykstra.
I doubled check everything and my connection string is perfect. What I failed to realize was I copied another set of appSettings which I already have. Look below
My advice is to check every inch of Web.config and I'm sure that is the culprit.
I had this problem too, I solved the problem by calling the base.onModelCreating in my DB context
I was getting the same error when I made some changes to my model .. Only way I was able to resolve was 1) stop/kill the process 2) clean solution and rebuild the solution
It seems a problem of connecting setting / inconsistent entry in via
Web.config
. To fix this issue , follow below steps:Remove connection related information (staring with <connectionStrings> from
Web.config
and remove models generated so far.Now generate the model and it will add fresh connection entry in
Web.config
file. Once model is generated, build the solution then start doing Scaffolding controlled. it will work.Using Visual Studio 2015
Upgraded mysql server and in the process the mysql for visual studios was upgraded from 6.9.7 to 6.9.8
In my web config there was still a reference to the old 6.9.7 version
Here is my git diff that solved the issue: