脚手架控制器不适用于visual studio 2013 update 2

时间:2014-05-28 11:17:20

标签: visual-studio-2013 asp.net-mvc-5 asp.net-mvc-scaffolding

问题:

我已更新到Visual Studio 2013更新2,现在我无法使用脚手架控制器。

问题不是项目特定的:当我尝试脚手架控制器时,我在ALL和ANY项目中出现以下错误:

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

在更新到Visual Studio 2013更新2之前,它正在运行。

已经将问题用Google搜索过,但各种建议都没有用到

例如:

  1. 在我的上下文中评论OnModelCreating;

  2. 删除MvcScaffolding等软件包(我没有安装,也不适用于任何项目);

  3. 我已修改/自定义了一些模板,但它在更改后正在运行。

    编辑:

    我卸载了Visual Studio 2013 Update 2,从而恢复为Visual Studio版本12.0.21005.1 REL。

    问题已经消失。因此,问题肯定在Update 2上。

    问题:

    是否有人(包括Microsoft)知道修复?

    编辑2:

    Farruk Subhani的回答没有解决这个问题:问题清楚地表明删除对MVCScaffolding的引用并不能解决问题。

    我已经加了200点赏金,请明确说明问题。

15 个答案:

答案 0 :(得分:35)

嘿,对于你们所有人都没有用的,真正的答案是你需要删除在web.config上有一个configSource的ANYTHING,并且需要内联连接字符串。

修改

有人指出,只有<configSettings><appSettings><connectionStrings>标记不使用configSource属性。并且他仍然可以在其他地方使用configSource属性,例如重写器标记。

我认为工具不能跟随configSource位置使用它所使用的东西,如连接字符串和应用程序设置。

如果尚未修复,微软应该处理这个问题。

编辑2:

甚至认为@awrigley已经将他的答案标记为正确,这是一个已知的Visual Studio错误。我已经设法告诉我们,我认为很快就会得到一些关注。 https://github.com/aspnet/Tooling/issues/169#issuecomment-144197015

答案 1 :(得分:12)

事情的组合对我有用:

  1. 升级到Visual Studio 2013 Update 3.

  2. 将实体框架升级到6.1.1

  3. 修改上下文配置以使用IDbSet&lt; ...&gt;而不是DbSet&lt; ...&gt; (我听说这会影响使用异步操作,但在我的情况下显然不会,因为我在我的登录操作中使用它,等等,由ASP.NET Identity 2示例Nuget包提供)。

  4. 为什么这种组合有效,我不知道。但是,鉴于MS的雷鸣般的沉默,我可能并不孤单。我想更新2只是没有工作......

答案 2 :(得分:6)

<强>解决方案

确定

部分
<connectionStrings>..</connectionStrings>

之后
<configSections>..</configSections>

答案 3 :(得分:4)

请在程序包管理器控制台中运行以下命令:

Uninstall-Package EntityFramework -Force

Install-Package EntityFramework

Uninstall-Package  MvcScaffolding

Install-Package MvcScaffolding

答案 4 :(得分:3)

我在Visual Studio 2013 Update 3中遇到了同样的问题,但仅适用于使用Entity Framework的脚手架。 该问题似乎是由于Entity Framework 6.1.0与Visual Studio 2013 Update 2及更高版本中的脚手架之间不兼容造成的。

要升级EF,请执行以下操作:

  

Uninstall-Package EntityFramework -Force

     

Install-Package EntityFramework

这个答案来自here

升级后,脚手架对我来说工作正常。确保在需要实体框架的每个项目中安装新版本。

答案 5 :(得分:2)

在我的情况下,我用web.config中的连接字符串解决了这个问题。

预先解决我的问题

  <connectionStrings configSource="Configs\ConnectionString.config"/>

我不知道为什么,但是无法连接到数据库并失败。

更改后

<connectionStrings>
    <add name="UIBuilderContext" connectionString="metadata=res:/ ..... " />
  </connectionStrings>

并且有效

答案 6 :(得分:2)

对于尚未在其解决方案中安装任何脚手架nuget包的人来说,这可能非常有用。

实际上我没有安装mvcscaffolding或t4scaffolding并收到相同的错误消息。

在我的情况下,问题/错误是由更改连接字符串引起的。

这里有我的/重现的步骤。

  • 已安装的Visual Studio 2013社区版
  • 创建MVC项目
  • 创建代码优先模型
  • 编辑连接到真实服务器的连接字符串,如下所示:

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

然后我通过nuget启用了迁移,如下所示:

  • 启用-迁移
  • 添加迁移InitialCreate
  • 更新的数据库的
  • 我启动了网站,我可以注册一个用户。所有表格都已正确创建。

然后我使用scaffolding选项创建了一个控制器:

  • 右键点击“控制器”&gt; “添加”&gt;控制器...&gt;具有视图的MVC 5控制器,使用实体框架&gt;选择我的上下文和要使用的类。它奏效了。

然后我决定先做更多的代码更改并从头开始:

  • 我更改了连接字符串,如下所示,使用 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" />
    

然后我继续说道:

  • 删除了迁移文件夹
  • 使用与上述相同的命令在nuget控制台
  • 中重新启用迁移
  • 启动了网站并注册了用户
  • 检查了mdf db。所有表都在那里,因此连接字符串可以正常工作。
  • 右键点击“控制器”&gt; “添加”&gt;控制器...&gt;带有视图的MVC 5 Controller,使用Entity Framework。选择我的上下文和要使用的类。它没有用,出现了弹出错误:
  

运行所选代码生成器时出错:'异常有   被调用的目标所抛出。'

<强> SOLUTION:

经过一番调查后,我所做的是将web.config中的连接字符串更改回初始的连接字符串到“真实服务器”(而不是localdb)。我再次尝试生成带有视图的控制器。它奏效了!

所以在我看来,连接字符串问题/ bug或localdb问题......无法解释它。也许Visual Studio不喜欢我做的事情,我不得不保留旧的连接字符串......

无论如何,现在当我需要脚手架时,我只需将连接字符串更改为有效的连接字符串。然后,为了测试我的网站,我将其更改回localdb。

答案 7 :(得分:2)

我将在这里用英语解释一下,所以任何人都可以理解。希望这可以帮助那里的任何人这是因为Visual Studio无法连接到数据库模型。

当您更改扩展DbContext的类中的名称和/或路径并且未在Web.config文件中更改它时(在项目的最外层:根目录),会发生这种情况。

示例:

想象一下你搭建了DbContext代码:

a)右键单击项目中的文件夹并添加了“ADO.NET实体数据模型”,并将其命名为“Model1”

您将获得以下代码:

  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)现在,您已经确定您刚刚编写的名称非常糟糕,因此您将其更改为AppContext

您的代码现在看起来像这样:

  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

然后,您尝试使用视图来支持CRUD(创建,读取,更新,删除)操作,但它失败了!

enter image description here

为什么?

好吧,如果我们转到web.config文件,我们可以看到以下字符串:

 <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" />

(此行通常低于<add name="DefaultConnection"

enter image description here 问题出在哪里。您需要更改Model1以获得您给出的名称!

在这种情况下,它应该说“AppContext”而不是“Model1”

它说:

initial catalog=Skelleton.Models.Model1;

验证:

  1. 这是具有类

  2. 的.cs文件的名称
  3. 命名空间(或类名称前面的一系列名称(以点分隔))是正确的。重要的是要注意你没有附加“.cs”扩展名;只是你文件的名称。

  4. 应该如下所示: enter image description here

    因为我更改了类的名称,包括内部和外部(在它内部和它的文件名),并且更改了它的位置,我只是将其重命名为AppContext

    完成此操作后。你可以正常搭建;)

    希望这有帮助!

答案 8 :(得分:2)

对我来说,我必须确保<configSettings><appSettings>和&amp; <connectionStrings>标记使用configSource属性

我仍然可以使用configSource属性,例如rewriter标记。

答案 9 :(得分:2)

我认为,问题是由于web.config文件中的配置错误。

在我的情况下,我有多个 web.config中的<entityFramework>个部分,我在更改配置后解决了问题。

答案 10 :(得分:2)

我正在使用VS 2013 Update 4,但问题完全相同。将连接字符串从外部文件移动到web.config时,它适用于我。所以我猜你可以尝试确保在scaffolding时不使用configureString的configureSource属性。

我的web.config在更改之前和之后 之前:

<connectionStrings configureSource="connectionStrings.config/>

后:

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

答案 11 :(得分:1)

我已执行以下操作来解决此问题:

  1. 在程序包管理器中检查您是否拥有MVCScffolding或T4Scaffolding(vs2013的核心或预发行版)
  2. 卸载所有依赖于任何MVCScaffolding或T4Scaffolding的打包
  3. 完整复制CodeTemplates文件夹(不要替换自定义模板,但请确保您从该文件夹的根目录中获取其余文件。我认为您已经完成了该部分,因为您已经自定义了一些模板,因此您知道此文件夹的位置是)
  4. 构建项目并关闭它。
  5. 在管理模式下重新启动Visual Studio
  6. 打开软件包管理器控制台(它不应该有任何错误,您可以看到PM&gt;没有错误。
  7. 右键单击Controller文件夹并选择Add-&gt; New Scaffolding Item或选择Add-&gt; Controller都应该询问你想要哪一个,然后选择MVC5脚手架或列表中出现的那个。
  8. 如果您的控制器可能需要选择DBContext或存储库等,您将可以选择参数。
  9. 这为我制作了控制器和相关视图。

    我在该文件夹中添加了一个名为QuickView的自定义部分视图,但是这个脚手架程序没有考虑到这一点,并且只生成默认情况下正在执行的视图。我不确定您是否需要在文件中添加这些自定义视图以告知Scaffolder也生成这些视图。

答案 12 :(得分:0)

我在更新4中遇到了同样的问题。我发现问题来自<configSections></configSections>没有定义部分名称。我在标签之间插入的修复如下,所有错误都已修复:

<section name="entityFramework" 
         type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, 
               EntityFramework, Version=6.0.0.0, Culture=neutral, 
               PublicKeyToken=b77a5c561934e089" requirePermission="false" />

构建项目,一切都应该有效。干杯!

答案 13 :(得分:0)

我在最后一次切入之前运行VS 2015RC。不要在这里打折任何解决方案。我的解决方案是在nugget包管理器下并更新了我的Microsoft.Aspnet.Mvc 5.2.3包并解决了我的问题。 我希望能帮助任何使用VS 2015的人。

答案 14 :(得分:0)

我使用了Initializer,当从myDbContext构造函数中删除/注释Initializer时,脚手架工作正常

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

        //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DastanakDbContext>());
        //Database.Initialize(true);
    }