Scaffolding controller doesn't work with visua

2019-01-05 00:17发布

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.

16条回答
劳资没心,怎么记你
2楼-- · 2019-01-05 01:03

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-05 01:06

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.

查看更多
甜甜的少女心
4楼-- · 2019-01-05 01:09

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.

查看更多
迷人小祖宗
5楼-- · 2019-01-05 01:09

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 the configSections, 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).

查看更多
Lonely孤独者°
6楼-- · 2019-01-05 01:12

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.

查看更多
不美不萌又怎样
7楼-- · 2019-01-05 01:12

i used Initializer and when remove/comment Initializer from myDbContext constructer , scaffolding work fine

public myDbContext () : base("name=DefaultConnection")
    {

        //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DastanakDbContext>());
        //Database.Initialize(true);
    }
查看更多
登录 后发表回答