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

后端 未结 16 2961
别跟我提以往
别跟我提以往 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 03:54

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

    0 讨论(0)
  • 2020-11-27 03:55

    Please run the following command in the Package Manager Console:

    Uninstall-Package EntityFramework -Force
    
    Install-Package EntityFramework
    
    Uninstall-Package  MvcScaffolding
    
    Install-Package MvcScaffolding
    
    0 讨论(0)
  • 2020-11-27 04:00

    For me, I had to ensure <configSettings>, <appSettings>, & <connectionStrings> tags were NOT using a configSource attribute.

    I was still able to use configSource attributes else where, such as the rewriter tag.

    0 讨论(0)
  • 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:

      <add name="DefaultConnection"
           connectionString="server=myserv;database=MyCustomerDB;user id=myuser;password=mypass" 
           providerName="System.Data.SqlClient" />
      

    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:

      <add name="DefaultConnection" 
           connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Test-20141126094523.mdf;Initial Catalog=aspnet-Test-20141126094523;Integrated Security=True"
           providerName="System.Data.SqlClient" />
      

    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.

    0 讨论(0)
  • 2020-11-27 04:02

    Solution

    Make sure section

    <connectionStrings>..</connectionStrings>
    

    is after

    <configSections>..</configSections>
    
    0 讨论(0)
  • 2020-11-27 04:03

    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.

    0 讨论(0)
提交回复
热议问题