PROBLEM:
I have updated to Visual Studio 2013 update 2 and now I cannot scaffold controllers.
The problem is not project specific: when I try to scaffold a controller, I get the following error in ALL and ANY project:
There was an error running the selected code generator:
'Exception has been thrown by the target of an invocation.'
It was working before updating to Visual Studio 2013 update 2.
Have googled the issue to death, but none of the various suggestions work
Eg:
Commenting out OnModelCreating in my context;
Removing packages such as MvcScaffolding, etc (I have none installed and it doesn't work with ANY project);
I have modified/customised some of the templates, but it was working after the changes.
EDIT:
I uninstalled Visual Studio 2013 Update 2 and thereby reverted to Visual Studio version 12.0.21005.1 REL.
The problem has disappeared. Therefore, the problem is quite definitely with Update 2.
QUESTION:
Does anyone (including Microsoft) know of a fix?
EDIT 2:
Farruk Subhani's answer does not address the question: The question clearly states that removing references to MVCScaffolding does not solve the issue.
I have added a 200 point bounty, please address the question as clearly stated.
I think, the problem is due to bad configuration in web.config file.
In my case I had multiple
<entityFramework>
sections in web.config, and the problem resolved after I changed the configurations.I had the very same issue with Visual Studio 2013 Update 3, but only for the scaffolders working with Entity Framework. The issue is seemingly caused by the incompatibility between Entity Framework 6.1.0 and the scaffolders in Visual Studio 2013 Update 2 and above.
To upgrade EF do the following:
This answer is borrowed from here
After the upgrade the scaffolders are working fine for me. Make sure to install the new version in every project where Entity Framework is required.
This can be useful for people who haven't installed any scaffolding nuget packages in their solution.
In fact I don't have mvcscaffolding or t4scaffolding installed and got the same error message.
In my case the problem/bug was caused by changing the connection string.
Here what I had/steps to reproduce.
Edited connection string to connect to a real server, like this:
Then I enabled migrations via nuget, like this:
Then I created a controller by using the scaffolding option:
Then I decided to do more code first changes and begin from scratch:
I changed the connection string as follows, to use localdb:
Then I went on:
SOLUTION:
After some investigation, what I did, is changing back the connection string in the
web.config
to the initial one to the "real server" (instead of localdb). I tried again to generate the controller with views. It worked!So it seems to me a connection string problem/bug or a localdb problem... can't explain it. Maybe Visual Studio doesn't like what I did, I had to keep my old connection string...
Anyway, so now when I need scaffolding I just change the connection string to the one that works. Then to test my website I change it back to the localdb one.
For me what worked was to make sure the
<configSections>
node in the web.config file was the first node immediately after<configuration>
node.Initially, when I added my
connectionStrings
, I had actually placed it before theconfigSections
, which made the Scaffold tool break.Seems like when the scaffolding runs and tries to grab connection info, it expects the config section for the entityFramework node to be there already so it know what DB provider to use, but when I had the connection string before configSections, it didn't know to use LocalDB (which is what my conn string was using).
I'm running VS 2015RC the last one before final cut. Not to discount any of the solutions here. My solution was under nugget package manager and updated my Microsoft.Aspnet.Mvc 5.2.3 package and that fix my issue. I hope that helps anyone using VS 2015.
i used Initializer and when remove/comment Initializer from myDbContext constructer , scaffolding work fine