Scaffolding controller doesn't work with visual studio 2013 update 2

54,075

Solution 1

A combination of things have worked for me:

  1. Upgrade to Visual Studio 2013 Update 3.

  2. Upgrade Entity Framework to 6.1.1

  3. Modify the context configuration to use IDbSet<...> instead of DbSet<...> (I have heard that this can affect using async actions, but not apparently in my case, as I use this in my login actions, etc, as supplied by ASP.NET Identity 2 sample Nuget package).

Quite why this combination works, I have no idea. But then given the thundering silence from MS, I am probably not alone. I guess update 2 just didn't work...

Solution 2

Hey for all of you that nothing works, the real answer is you need to remove ANYTHING that has a configSource on the web.config and the connection string needs to be inlined.

EDIT:

Someone pointed out that it needs to be only <configSettings>, <appSettings>, and <connectionStrings> tags NOT using a configSource attribute. And that he was still able to use configSource attributes elsewhere, such as the rewriter tag.

I think it's that the tooling cannot follow configSource locations for the stuff it uses like connection strings and application settings.

Microsoft should be on this issue if not fixed yet.

EDIT 2:

Even thought @awrigley has marked his answer as correct it's a known Visual Studio bug. I've managed to tell then and I think it will get some attention soon. https://github.com/aspnet/Tooling/issues/169#issuecomment-144197015

Solution 3

Solution

Make sure section

<connectionStrings>..</connectionStrings>

is after

<configSections>..</configSections>

Solution 4

Please run the following command in the Package Manager Console:

Uninstall-Package EntityFramework -Force

Install-Package EntityFramework

Uninstall-Package  MvcScaffolding

Install-Package MvcScaffolding

Solution 5

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:

Uninstall-Package EntityFramework -Force

Install-Package EntityFramework

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.

Share:
54,075
awrigley
Author by

awrigley

ASP.NET MVC and CMS

Updated on July 05, 2022

Comments

  • awrigley
    awrigley almost 2 years

    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:

    1. Commenting out OnModelCreating in my context;

    2. 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.

  • awrigley
    awrigley almost 10 years
    See point 2 in my question - I did not have any MVCScaffolding or T4Scaffolding installed. So your solution is not relevant. You don't mention if you have Visual Studio 2013 Update 2 installed - if you don't then you are not answering the question and you should remove your answer.
  • Farrukh Subhani
    Farrukh Subhani almost 10 years
    I have VS 2013 update 2 installed. If you dont have MVCScaffolding or T4Scaffolding even then check your packages folder it might have an empty folder. If none of them is installed you can start from step 3. I had none of these installed and had this issue when i updated VS to update 2. I had to copy all Templates (deleted VB ones only) and kept all files as it is and followed above and it worked. Then I installed MVCMailer and Package Manager gave errors on init so had to remove them and retry and it worked again.
  • Farrukh Subhani
    Farrukh Subhani almost 10 years
    I have added MVCMailer package today and it added T4Scaffolding.Core again and I reproduced the same error, after removing both packages the error still remains and after i followed step 4-8 Scaffolder worked again.
  • awrigley
    awrigley almost 10 years
    I have looked at your suggestions in detail. Essentially, they do NOT address the issue that I have. They address the issue where MVCScaffolding or T4Scaffolding were installed. That is not my case. Steps 3 onwards are nothing other than the standard instructions for scaffolding controllers. That is what I do anyway. Sorry, this is definitely not the solution to my problem.
  • awrigley
    awrigley almost 10 years
    I will try that when I get time to reinstall VS 2013 Update 2
  • awrigley
    awrigley over 9 years
    The connection strings are fine, no problem there, so can rule that one out.
  • sming
    sming over 9 years
    FYI I'm on update 4 and these instructions sadly didn't work - same error.
  • awrigley
    awrigley over 9 years
    Have you tried the other suggestions on here? They didn't work for me, but if what works for me doesn't work for you, then maybe what doesn't work for me will work for you? Just hoping. Quite why the MS evangelists don't pick up on these problems, eg Scott Hanselman, and write a blog post with solutions is beyond me.
  • sming
    sming over 9 years
    yes I've tried them all barring Farrukh's, which you state is not really answering the question. regisbsb & Math's answers don't apply since we're using inline config strings.
  • rohancragg
    rohancragg over 9 years
    This fixed it for me. I had connStrings in a separate file for privacy reasons. Temporarily putting them back into the we,confi file (i.e. inline) fixed it for me (and I then moved them back as they were once I had the scaffolded controller and views).
  • rohancragg
    rohancragg over 9 years
    Yes. Fixed it for me anyway.
  • awrigley
    awrigley over 9 years
    I note that this does not answer the question, as my problem, as stated in the question, was different. I would not want anyone to be confused, so -1.
  • awrigley
    awrigley over 9 years
    I would suggest reading the FAQ on this site about how to use it. It is not intended to be a forum, with the thread swaying this way and that and diverging from the question. It is, instead, a Q&A site. Ie, you ask a question and other people suggest answers. They do not try to engage in a "conversation" about similar problems, as might happen on a forum. I don't intend to be rude or unfair, just trying to promote the correct use of this site as others did with me in the early days.
  • firepol
    firepol over 9 years
    ah ok I see, then I guess I should have published a same question, let somebody tell me it's a duplicate, then answer on my own question saying it's a different problem (same error message... different problem? Really?) and justify the duplicate because the solution is different? I love this website because I find solutions. And I don't like duplicates... anyway, thx for the feedback. I'll consider twice before posting working and useful solutions next time...
  • awrigley
    awrigley over 9 years
    sorry, but you very clearly don't see. The error is similar or even the same, but your set up is different. You were NOT answering the question that I had asked and that is confusing to others.
  • firepol
    firepol over 9 years
    I wrote in bold "for people who haven't installed any scaffolding nuget packages". That should not really confuse people who don't apply... anyway... sorry, feel free to delete the answer. When I googled for this VS bug I arrived here and in another similar stackoverflow question. Sometimes to find the good answer one needs to do many google searches... I was only thinking to help... next time I publish on my blog...
  • user510101
    user510101 over 9 years
    see my answer...essentially, ensure that in web.config, the configSections element is the first element in the configuration, before the connectionStrings element
  • radu florescu
    radu florescu over 9 years
    Yes, I had some unwanted stuff in web.config
  • Jose
    Jose over 9 years
    This solution worked for me, although I was having a different problem with the error message: "...To reference the existing configuration use the Entity<T>() or ComplexType<T>() methods." I had installed VS2013 Update 4 and EF 6.1.2. What I did differently is change the DbSet<...> to IDbSet<...> in T4 templates which you can add by right-clicking on the web project Entity Framework>Customize Reverse Engineer Templates.
  • ThdK
    ThdK over 9 years
    This sollution worked for me with Visual Studio Express 2013 for Web and EF 6.1.2. Thanks!
  • Geoff Gunter
    Geoff Gunter over 9 years
    Worked for me, I had both AppSettings and ConnectionStrings using configSource. Had to remove both to fix. Thank you!
  • awrigley
    awrigley over 9 years
    Sorry, but I had not changed the model name. There is no error in my web.config. As stated clearly in the question, the problem was produced by upgrading Visual Studio 2013 to Update 2.
  • Jose A
    Jose A over 9 years
    @awrigley: Ah Perfect :) . Hate when those things happen. Will let the answer in there just in case someone present a similar problem. Thanks for clarifying.
  • Shiham
    Shiham about 9 years
    worked for me on VS 2013 Update 4, just commented out it and worked perfectly
  • chris vietor
    chris vietor about 9 years
    That worked for me. Comment out the line with the configSource attribute and copy the content from the configSource file for scaffoling.
  • Brice
    Brice almost 9 years
    I had forgotten to add <connectionStrings> and </connectionStrings> around "<add name...". This points to the general solution being a webconfig error preventing the scaffolding to work.
  • ThrowingSpoon
    ThrowingSpoon almost 9 years
    I found I had a carelessly put my entity framework contexts outside of the entityFramework section in my web.config
  • Treyphor
    Treyphor almost 9 years
    Changing to use IDBSet instead of DbSet is what fixed it for me. Thanks!
  • hacker
    hacker over 8 years
    VS 2015 also has this problem
  • regisbsb
    regisbsb over 8 years
    It's a know tooling problem and a reported bug.
  • Laurence Frost
    Laurence Frost over 8 years
    Changing DbSet to IDbSet worked for me too. Thanks :)
  • sam1132
    sam1132 over 8 years
    My guess is this issue isnt
  • Steven Edison
    Steven Edison over 7 years
    Tried 50 different things and this one worked for me!
  • MusicAndCode
    MusicAndCode over 7 years
    The error is not the same "There was an error running the selected code generator: 'Exception has been thrown by the target of an invocation.'
  • Kevin LaBranche
    Kevin LaBranche about 7 years
    Just a mention that this is still an issue. I use config source for my connections and app settings. I inlined my connections just to see if it worked and did (VS15 update 3 & EF 6.1.3). I have to use config source. When scaffolding I just tell the controller creation to not do the EF for me and on view creation I don't set the data context. Works for me anyway as the default scaffolding is never what I want.
  • EoRaptor013
    EoRaptor013 about 7 years
    I'm having the problem with VS 2017! Three major versions of VS, and all have the same problem? Something's has to be wrong in the documentation or samples.
  • Tsagana Nokhaeva
    Tsagana Nokhaeva over 6 years
    I think this bug is left on purpose :) MS forces us to push our connection strings to VCS. There is no other way to explain this.
  • yibe
    yibe over 5 years
    Thank you very much!, you saved my day finally. This one worked to me!