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

后端 未结 16 3206
别跟我提以往
别跟我提以往 2020-11-27 03:54

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

16条回答
  •  一个人的身影
    2020-11-27 04:00

    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.

    • Installed Visual Studio 2013 Community Edition
    • Created MVC project
    • Created code first model
    • Edited connection string to connect to a real server, like this:

      
      

    Then I enabled migrations via nuget, like this:

    • Enable-Migrations
    • Add-Migration InitialCreate
    • Update-Database
    • I started the website and I could register a user. All tables were created correctly.

    Then I created a controller by using the scaffolding option:

    • right click on "Controllers" > "Add" > Controller... > MVC 5 Controller with views, using Entity Framework > selected my context and a class to be used. It worked.

    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:

    • deleted the migrations folder
    • re-enabled migrations using the same commands as above, in the nuget console
    • started thw website and registered a user
    • checked the mdf db. All tables are there, so the connection string works.
    • right click on "Controllers" > "Add" > Controller... > MVC 5 Controller with views, using Entity Framework. Selected my context and a class to be used. It did not work and this popup error appeared:

    There was an error running the selected code generator: 'Exception has been thrown by the target of an invocation.'

    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.

提交回复
热议问题