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:13

I'm on VS 2013 Update 4 and have exactly same issue. It works for me when moving connection string from external file into web.config. So I guess you could try to make sure not using configureSource attribute for connectionString when scaffolding.

My web.config before and after the change Before:

<connectionStrings configureSource="connectionStrings.config/>

After:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net configSource="log4net.config" />
  <connectionStrings>
    <clear/>
    <add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=YourDb;Integrated Security=False;User ID=sa;Password=YourPassword!#;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
查看更多
对你真心纯属浪费
3楼-- · 2019-01-05 01:16

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

查看更多
Animai°情兽
4楼-- · 2019-01-05 01:16

I have performed following to solve this issue:

  1. In Package Manager check if you have MVCScffolding or T4Scaffolding (Core or pre release for vs2013)
  2. Uninstall all packaged that depend on any of MVCScaffolding or T4Scaffolding
  3. Copy CodeTemplates Folder in full (Do not replace your custom templates but make sure you have rest of the files from root of this folder. I take that you have done that part as you have customised some templates so you know where this folder is)
  4. Build Your project and close it.
  5. Restart Visual Studio under admin mode
  6. Open Package Manager Console (it should not have any errors and you can see PM> without errors.
  7. Right Click on Controller folder and choose Add->New Scaffolding Item or Choose Add->Controller both should ask which one you want and then choose MVC5 scaffold or which ever appears in list.
  8. You will then get to select your parameters if its controller you might need to choose DBContext or repository etc.

This produced the controller and relevant views for me.

I added a custom partial view called QuickView in that folder however this scaffolding procedure did not consider that and only generated views that it was doing by default. I am not sure if you need to add these custom views in a file to tell Scaffolder to generate those as well.

查看更多
祖国的老花朵
5楼-- · 2019-01-05 01:22

I'll explain here a little bit more in English, so anyone can understand. Hope this helps anyone out there This occurs because Visual Studio fails to connect to the database model .

This happens when you change the name and/or the path in the class that extends DbContext and didn't change it in the Web.config file (at the outermost part of your project: the root).

Example:

Imagine you scaffolded the DbContext code:

a) You right clicked a folder in your project and added a "ADO.NET Entity Data Model", and you named it "Model1"

You get the following code:

  public class Model1 : DbContext
{
    // Your context has been configured to use a 'Model1' connection string from your application's 
    // configuration file (App.config or Web.config). By default, this connection string targets the 
    // 'Skelleton.Models.Model1' database on your LocalDb instance. 
    // 
    // If you wish to target a different database and/or database provider, modify the 'Model1' 
    // connection string in the application configuration file.
    public Model1()
        : base("name=Model1")
    {
    }

    // Add a DbSet for each entity type that you want to include in your model. For more information 
    // on configuring and using a Code First model, see http://go.microsoft.com/fwlink/?LinkId=390109.

    // public virtual DbSet<MyEntity> MyEntities { get; set; }
}

enter image description here

b) Now, you have decided that the name you've just written is plain bad, so you change it to AppContext

Your code now looks like this:

  public class AppContext : DbContext
{
    // Your context has been configured to use a 'AppContext' connection string from your application's 
    // configuration file (App.config or Web.config). By default, this connection string targets the 
    // 'Skelleton.Models.AppContext' database on your LocalDb instance. 
    // 
    // If you wish to target a different database and/or database provider, modify the 'AppContext' 
    // connection string in the application configuration file.
    public AppContext()
        : base("name=AppContext")
    {
    }

    // Add a DbSet for each entity type that you want to include in your model. For more information 
    // on configuring and using a Code First model, see http://go.microsoft.com/fwlink/?LinkId=390109.

    // public virtual DbSet<MyEntity> MyEntities { get; set; }
}

enter image description here

Then, you try to Scaffold the CRUD (Create, Read, Update, Delete) operations with views and it fails!

enter image description here

Why is that?

Well, if we go to the web.config file, we can see the following string:

 <add name="Model1" connectionString="data source=(LocalDb)\v11.0;initial catalog=Skelleton.Models.Model1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

(This line is usually below <add name="DefaultConnection" )

enter image description here And there is where the problem lies. You need to change Model1 for the name you've given out!

In this case, it should say "AppContext" instead of "Model1"

And where it says:

initial catalog=Skelleton.Models.Model1;

Verify that:

  1. It's the name of the .cs file that has the class

  2. The namespace (or the series of names (dot-separated) that comes before the name of your class) is the correct one. It's important to notice that you do not append to the end the ".cs" extension; just the name of your file.

It should look like this: enter image description here

Because I changed the name of the class, both internally and externally (inside it and it's file name), and did not change its location, I just rename it to AppContext

After this has been done. You can scaffold normally ;)

Hope this helps!

查看更多
登录 后发表回答