Application can't scaffold items
Solution 1
I had this problem too, I solved the problem by calling the base.onModelCreating in my DB context
base.OnModelCreating(modelBuilder);
Solution 2
In my case I moved my connection strings out of the Web.config to
<connectionStrings configSource="ConnectionStrings.config"/>
that when I started getting the error when I was trying to scaffold.
There was an error running the selected code generator: ‘Exception has been thrown by the target of an invocation.’
Moving my connection strings back to the Web.config solved my issue.
Solution 3
I had faced the same problem while creating controller using scaffold with 'ASP.NET MVC5 using views with Entity Framework'
The problem was because I provided <connectionStrings>
tag before <configSections>
in web.config. setting <connectionStrings>
after <configSections>
resolved the issue.
I guess, while scaffolding, ASP.NET MVC want to resolve the Entity Framework first, then the connection string, as I have provided connection string earlier so that after resolving the Entity Framework version it could not find the connection string so it thrown invocation problem.
Solution 4
This solved the issue for me,
Adding throwIfV1Schema: false
to base of DbContext
As so:
public MyDbContext() : base("ConectionStringName", throwIfV1Schema: false) { }
Solution 5
Late reply; but, I am posting this answers with the hope that somebody can use this answer to solve their problem.
For some reason, if your program CANNOT read connectionString information (either from web.config or from other means) then, this error will be thrown.
Make sure that valid connectionString information is being retrieved properly without any problem.
Comments
-
runamuk0 almost 2 years
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?
-
sam starobin about 9 yearsthis worked for me to, must be some configuration change it makes (but where, in the solution or one of the config files?)
-
Enzero almost 9 years@JosephCasey I don't know but you must have some kind of dependency somewhere which is stopping you from uninstalling check what packages you have installed and see what dependencies they have. You will have to uninstall them first before removing EntityFramework.
-
Enzero almost 9 years@JosephCasey are you by chance using the beta version of Entity Frameworks since that does have a dependency (EntityFramework.Commands). It might be better to just use the current version at date being 6.1.3.
-
Joseph Casey almost 9 yearsI have EF 6.1.3. I can try to check it later. I think the bigger problem was my out-of-date approach.
-
Aaron almost 9 yearsThis fixed the problem for me. Put this line of code at the very end of the OnModelCreating method after all the entities have bee configured. VS2013 Update 4
-
David McSpadden over 8 yearshad duplicate connection string entries in my web.config
-
Gabriel Sadaka over 8 yearsI had the exact same problem, for some reason it doesn't like the EntityTypeConfiguration class, thanks!
-
Hemant over 8 yearsI had declared the connection strings before the configSections node. So the scaffolder was not able to find EF.
-
Stack0verflow over 8 yearsI have this line in my code to begin with, but I am still having the error when adding new scaffolded items. Using VS 2013 Update 5 and SQL Server Express 2012 and following the tutorial at asp.net/mvc/overview/getting-started/…
-
Andy V about 8 yearsBOOOO MSFT. This was my issue.
-
FuckStackoverflow almost 8 yearsI often pride myself on not doing stupid things. This, today, was one of a few. Good catch and call.
-
lordstyx about 7 yearsDon't forget to rebuild after doing this!
-
Jackson Lopes almost 7 yearsI too had the connectionstring before the ConfigSections. Moving it after the ConfigSections solved it for me. Cheers !!!
-
Phi over 6 years@DavidMcSpadden bro, you sould post your comment as a new response. After hours I solved my error because of you. Thanks!
-
Tsagana Nokhaeva about 6 yearsYeah, that was it! Thanks! But how to separate web.config into 2 files without this pain?
-
Tahir Hassan almost 6 yearsI have my context and model classes in a separate project and this worked for me too. Thanks.
-
Eric Sassaman over 5 yearsThe simple advice "check relationships" led me in the right direction. In my case I had a bad [ForeignKey()] decorator on an EF class. Once that was fixed, problem solved.
-
yibe over 5 yearsTo me, your answer before your two updates solved the issue. I just used double backslashes (\\) in the place where single backslash (\) was used, and it finally worked thank you.
-
Avinash Barnwal almost 4 yearsThis Solved the problem for me in VS 2019 ASP.NET (Full Framework) MVC Application. Using Entityframework 6.