Visual studio 2013 ollers“>”Add“>Controller…>MVC 5控制器,带视图,使用实体框架。选择了我的上下文和要使用的类。它不起作用,出现此弹出错误:

Visual studio 2013 ollers“>”Add“>Controller…>MVC 5控制器,带视图,使用实体框架。选择了我的上下文和要使用的类。它不起作用,出现此弹出错误:,visual-studio-2013,asp.net-mvc-5,asp.net-mvc-scaffolding,Visual Studio 2013,Asp.net Mvc 5,Asp.net Mvc Scaffolding,运行所选代码生成器时出错:“异常已发生。” 已由调用的目标抛出。” 解决方案: 经过一些调查,我所做的是将web.config中的连接字符串改回“real server”(而不是localdb)的初始字符串。我再次尝试生成带有视图的控制器。成功了 所以在我看来,这似乎是一个连接字符串问题/bug或localdb问题…无法解释。可能Visual Studio不喜欢我所做的,我不得不保留我的旧连接字符串 无论如何,现在当我需要脚手架时,我只需将连接字符串更改为有效的连接字符串。然后为了测试我的网站,

运行所选代码生成器时出错:“异常已发生。” 已由调用的目标抛出。”

解决方案:

经过一些调查,我所做的是将
web.config
中的连接字符串改回“real server”(而不是localdb)的初始字符串。我再次尝试生成带有视图的控制器。成功了

所以在我看来,这似乎是一个连接字符串问题/bug或localdb问题…无法解释。可能Visual Studio不喜欢我所做的,我不得不保留我的旧连接字符串


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

对我来说,有效的方法是确保web.config文件中的
节点是紧接
节点之后的第一个节点

最初,当我添加我的
连接字符串
时,我实际上将其放在了
配置节
之前,这使得Scaffold工具中断


似乎当脚手架运行并试图获取连接信息时,它希望entityFramework节点的配置部分已经存在,这样它就知道要使用哪个DB提供程序,但当我在configSections之前有连接字符串时,它不知道要使用LocalDB(这就是我的conn字符串所使用的).

我将在这里用英语多解释一点,以便大家都能理解。希望这对其他人有所帮助,因为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; }
}
核实:

  • 它是包含类的.cs文件的名称

  • 名称空间(或类名称之前的一系列名称(以点分隔)是正确的名称空间。请注意,不要将“.cs”扩展名追加到末尾,而只追加文件名

  • 应该是这样的:

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

    完成后,您可以正常搭建;)


    希望这有帮助!

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

    <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" />
    
    Uninstall-Package EntityFramework -Force
    
    Install-Package EntityFramework
    
    Uninstall-Package  MvcScaffolding
    
    Install-Package MvcScaffolding
    

    对我来说,我必须确保
    ,&
    标记是而不是使用
    configSource
    属性


    我仍然能够在其他地方使用
    configSource
    属性,例如
    rewriter
    标记。

    我在更新4中遇到了同样的问题。我发现问题来自
    没有定义节名。我在标记之间进行的修复如下,所有错误都已修复:

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

    构建项目,一切都应该正常。干杯!

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

    在我的情况下,我有多个
    部分,在我更改配置后问题得到解决。

    我正在使用VS 2013 Update 4,并且有完全相同的问题。将连接字符串从外部文件移动到web.config时,它对我有效。因此,我猜您可以尝试确保在scaffoldi时不使用connectionString的configureSource属性ng

    更改前后的My web.config 之前:


    我正在运行VS 2015RC,这是最终剪切前的最后一个版本。这里的任何解决方案都不打折扣。我的解决方案在nugget package manager下,更新了我的Microsoft.Aspnet.Mvc 5.2.3包,解决了我的问题。
    
    我希望这能帮助任何使用VS 2015的人。

    解决方案

    请确保部分

    。。
    

    <configSections>..</configSections>
    
    。。
    
    我使用了初始值设定项,当从myDbContext构造函数中删除/注释初始值设定项时,脚手架工作正常

    public myDbContext () : base("name=DefaultConnection")
        {
    
            //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DastanakDbContext>());
            //Database.Initialize(true);
        }
    
    公共myDbContext():base(“name=DefaultConnection”) { //SetInitializer(新的DropCreateDatabaseIfModelChanges()); //数据库初始化(true); }
    请参见我问题中的第2点-我没有安装任何MVCScapfolding或T4Scaffolding。因此,您的解决方案不相关。您没有提及是否安装了Visual Studio 2013 Update 2-如果没有,则您没有回答问题,您应该删除您的答案。我安装了VS 2013 Update 2。如果您没有安装MVCScaffolding或T4Scaffolding即使检查您的packages文件夹,它也可能有一个空文件夹。如果没有安装任何一个,您可以从步骤3开始。我没有安装任何这些,并且在更新VS以更新2时出现此问题。我必须复制所有模板(仅删除VB模板)然后我安装了MVCMailer,Package Manager在init上出现了错误,所以我不得不删除它们,然后重试,它再次工作。我今天添加了MVCMailer包,它添加了T4.Core,我再次复制了相同的错误,在删除两个包后,错误仍然存在,并且仍然存在之后,我按照第4-8步的步骤再次工作。我已经详细查看了您的建议。基本上,它们没有解决我遇到的问题。它们解决了安装MVCScapfolding或T4Scaffolding的问题。这不是我的情况。第3步之后的步骤只不过是脚手架的标准说明
      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; }
    }
    
     <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" />
    
    initial catalog=Skelleton.Models.Model1;
    
    Uninstall-Package EntityFramework -Force
    
    Install-Package EntityFramework
    
    Uninstall-Package  MvcScaffolding
    
    Install-Package MvcScaffolding
    
    <section name="entityFramework" 
             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, 
                   EntityFramework, Version=6.0.0.0, Culture=neutral, 
                   PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    
    <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>
    
    <connectionStrings>..</connectionStrings>
    
    <configSections>..</configSections>
    
    public myDbContext () : base("name=DefaultConnection")
        {
    
            //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DastanakDbContext>());
            //Database.Initialize(true);
        }