应用程序不能脚手架项目

我在VS 2013 Professional中创build了一个MVC 5应用程序,然后使用EF 6.1代码和SQL Server Express上的现有数据库。 当我尝试创build视图时,我正在使用“新的脚手架项目…”,然后使用entity frameworkselect“具有视图的MVC 5控制器”。我select模型和上下文类,然后单击确定。 然后出现以下错误消息,并且不创build代码。 我用相同的错误卸载了EF Power Tools。

错误

运行选定的代码生成器时发生错误:“exception已被调用的目标引发”。

我也尝试卸载/重新安装VS 2013和SQL Server没有任何更改。

任何其他想法可能会导致此错误?

我也有这个问题,我通过调用我的数据库上下文中的base.onModelCreating解决了这个问题

 base.OnModelCreating(modelBuilder); 

在我的情况下,我把我的连接string移出Web.config

 <connectionStrings configSource="ConnectionStrings.config"/> 

当我尝试搭脚手架时,当我开始发现错误时。

运行选定的代码生成器时发生错误:“exception已被调用的目标引发”。

将连接string移回Web.config解决了我的问题。

迟回复; 但是,我发布这个答案,希望有人可以使用这个答案来解决他们的问题。

由于某种原因,如果你的程序不能读取connectionString信息(无论是从web.config或其他方式),那么这个错误将被抛出。

确保正确检索有效的connectionString信息,没有任何问题。

这为我解决了这个问题,

throwIfV1Schema: false添加到DbContext

如此:

 public MyDbContext() : base("ConectionStringName", throwIfV1Schema: false) { } 

在使用“使用Entity Framework的视图的ASP.NET MVC5”创build使用脚手架的控制器时,我遇到了同样的问题

问题是因为我在web.config中的<configSections>之前提供了<connectionStrings>标签。 在<configSections> <connectionStrings>之后设置<connectionStrings>解决问题。

我猜,虽然脚手架,ASP.NET MVC想先解决entity framework,然后连接string,因为我已经提供了连接string,以便解决entity framework版本后,它无法find连接string,因此它引发调用问题。

我有相同的错误信息。 我试图从添加控制器对话框中添加一个新的Data context class ,然后我得到了一个不同的错误:

 There was an error running the selected code generator: 'Sections must only appear once per config file. See the help topic <locations> for exceptions. 

事实certificate,我有我的web.config文件中的两个<connectionStrings>元素。 (我从包含我的entity framework模型的类库的app.config中粘贴了一个。)

在我的情况下,我不得不恢复我的DBContext构造函数只使用静态连接string,如web.config中定义的

当我使用dynamic创build的连接string时,脚手架不工作。

 public MyContext() : base("name=MyContext") { } 

这可能有助于解决您的错误。

在我的OnModelCreating中,我为每个实体做了这个:

 modelBuilder.Configurations.Add(new EntityTypeConfiguration<EntityModel>); 

当我改变它到以下我停止得到你收到的错误。

 modelBuilder.Entity<EntityModel>(); 

我有同样的问题,它不会添加脚手架项目似乎卸载通过nuget包pipe理器控制台的entity framework工程

运行程序包pipe理器控制台

工具 – > NuGet程序包pipe理器 – >程序包pipe理器控制台

去除:

UnInstall-Package EntityFramework

重新安装:

Install-Package EntityFramework

我希望这可以帮助一个人,因为它花了我一阵子来这个。

我尝试了上面的大部分,没有运气。

终于成功的是:

  1. 删除以前dynamic创build的string条目
  2. 删除我的模型(保留所有控制器和视图)
  3. 重新创buildADO.NET实体数据模型 – 使用相同的名称创build一个与以前相同的名称的新的连接string条目

然后所有事情都再次奏效,减去3小时的开发时间。

  1. 重新(从备份)添加我的所有属性属性到表/实体类

似乎与dynamic创build的连接string和模型有关。 希望对可能发生的事情有任何想法。

在我的情况下,我解决了与web.config中的连接string的问题。

上一个问题我有<connectionStrings configSource="Configs\ConnectionString.config"/>

我不知道为什么,但vs不能连接到数据库,并失败。

改变之后

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

它的工作

我的解决scheme和将Webconfiguration中的连接string名称更改为DefaultConnection一样简单。 即使我的dbContext有另一个名字!

花了我2个小时找出这个废话!

这似乎是通过Web.config连接设置/不一致的条目的问题。 要解决此问题,请按以下步骤操作:

Web.config删除与连接有关的信息(注意<connectionStrings>),并删除到目前为止生成的模型。

现在生成模型,它将在Web.config文件中添加新的连接条目。 一旦生成模型,build立解决scheme,然后开始做脚手架控制。 它会工作。

当我对模型进行一些更改时,我得到了同样的错误。唯一能够解决的方法是1)停止/终止进程2)清理解决scheme并重build解决scheme

如果您有任何机会,请遵循Tom Dykstra的“使用MVC 5的entity framework6代码优先入门”。

我加倍检查一切,我的连接string是完美的。 我没有意识到的是我复制了另一套我已经有的appSettings。 往下看

  <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> 

我的build议是检查每一寸Web.config,我相信这是罪魁祸首。

在我的情况下,NuGet将以下提供程序添加到web.config中:

 <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact"/> 

当我改变提供者

 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/> 

它解决了这个问题(花了我大约5个小时,但要弄清楚:-()

我有同样的错误。 以下是我解决这个问题的两件事情:

  1. 在我的上下文中添加名称base:base(“name = connectionstringname”)
  2. 我在连接string中犯了一个错误,并修复了它。

我也遇到了这个问题,使用VS 2015.我尝试了所有其他的解决scheme在这里没有成功。 事实certificate,我的连接string(虽然forms正是MS告诉我们如何形成它)需要双\正常工作。

这是什么不是工作:

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

这里是我改变它为了让代码生成器工作:

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

注意V11.0前面的double \字符和数据库名称。

希望这能节省一些人的时间。

UPDATE

这使代码生成工作,但然后应用程序将不会运行,因为\ V11.0不是一个有效的连接string。 这在我看来像MS分析连接string时,在他们的代码生成有一个错误。 在我运行代码生成器以使应用程序再次运行之后,我必须将其更改回单个\。

更新2

经过我的合作伙伴更多的挖掘,我们发现真正搞砸了代码生成的事实是我们已经改变了“初始目录”字段。 当向导创build项目时,它会自动将初始目录设置为aspnet–。 然后我们把这个Initial Catalog字段改为DB。 这对于运行的应用程序非常有用。 它可以到数据库就好了。 但由于某种原因,这个代码脚手架生成螺丝。 通过把初始目录放回原来的状态(aspnet–,脚手架再次开始工作(不需要\ V11.0)。

希望在未来帮助别人。

检查实体之间的关系或其他模型devise问题。 要进行testing,请创build新的类模型,而不使用任何关系,并使用Scaffold生成控制器和视图。 为我工作。

这是(我99%肯定)连接string问题。 一旦我解决了Web.Config的问题,错误消失了。 我在另一个项目中首先使用数据库,一旦我从数据库的第一个连接string从App.config中复制到web.config(使用相同的名称),它按预期工作。

使用Visual Studio 2015

升级的MySQL服务器,在这个过程中,视觉工作室的MySQL从6.9.7升级到6.9.8

在我的网页configuration中还是有一个旧的6.9.7版本的参考

这是我的git diff解决了这个问题:

 - <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,Version=6.9.7.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"></provider> + <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,Version=6.9.8.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"></provider> 

我意识到这个问题现在已经很老了,但我想我会发布什么解决我的问题,以防以后帮助任何人。 就我而言,这是其他答案中提到的几件事情的组合。 为了确保我的连接string,我有…

  1. 将连接string移出Web.config文件
  2. 将服务器名称和密码移至Web.config的AppSettings部分中引用的单独文件
  3. 使用SqlConnectionStringBuilder将元素放在一起,然后将其传递给我的上下文类构造函数

这样做后,我无法创build更多的控制器。 要解决这个问题,我必须…

  1. 将完整的连接string放回到Web.config中,并除去对外部连接string文件的引用
  2. 添加一个无参数的构造函数到我的上下文类,并给它的名字我的connectionString像这样: public contextClass() : base("name=connectionStringName") { }

  3. 重build解决scheme,再次创build控制器,它的工作!

大家。 我知道我有点晚了,但我认为这仍然是有效的分享我的经验与这个问题。

我在两个项目中遇到了这个消息,两种情况下都是连接string。

在第一种情况下,它是“InitialCatalog”而不是“Initial Catalog”(分开)。

在第二种情况下,服务器名称(数据源参数)是错误的。

我希望它有帮助。

最好的祝福。

出于某种原因,当我在web.config中的<configSections>之间注释oracle.manageddataaccess.client节时,它工作。 (VS 2015和ODT 12)

 <configSections> <section name="entityFramework" type" /> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <!--<section name="oracle.manageddataaccess.client" />--> </configSections> 

在我的情况下,问题是由外部应用程序设置文件引起的:

 <appSettings configSource="appSettings.config" /> 

将应用程序设置返回到web.config文件解决了问题。

 <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> 

使用:VS 2015 Community Edition和EF 6.1.3也实施:种子方法,具有个性化的类,并在web.config文件中configuration,以便每次模型更改时运行。 这似乎是与web.config文件中的一些错误configuration有关,在我的情况下,就像我在这篇文章中看到的一些与该文件的其他部分一样的情况下,该部分重复了不同的内容,但是它重复了主标签当然。 这个部分不合时宜的部分,也是造成同​​样的行为和狭窄的信息,而尝试脚手架的原因。

你的上下文类可能会抛出一个exception。

我正在“C#6.0和.NET 4.6框架”一书中继续讨论,数据访问层部分的最后一个练习之一是添加日志logging。 那么,我想这个脚手架精灵会炸掉的。 可能sqllog.txtHttpRuntime文件权限没有定义…谁知道。 当我评论所有这些东西,它再次工作。

 namespace AutoLotDAL.EF { using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.Infrastructure.Interception; using AutoLotDAL.Interception; using AutoLotDAL.Models; using System; using System.Data.Entity.Core.Objects; using System.Web; public class AutoLotEntities : DbContext { //static readonly DatabaseLogger databaseLogger = // new DatabaseLogger($"{HttpRuntime.AppDomainAppPath}/sqllog.txt", true); public AutoLotEntities() : base("name=AutoLotConnection") { ////DbInterception.Add(new ConsoleWriterInterceptor()); //databaseLogger.StartLogging(); //DbInterception.Add(databaseLogger); //// Interceptor code //var context = (this as IObjectContextAdapter).ObjectContext; //context.ObjectMaterialized += OnObjectMaterialized; //context.SavingChanges += OnSavingChanges; } //void OnObjectMaterialized(object sender, // System.Data.Entity.Core.Objects.ObjectMaterializedEventArgs e) //{ //} //void OnSavingChanges(object sender, EventArgs eventArgs) //{ // // Sender is of type ObjectContext. Can get current and original values, // // and cancel/modify the save operation as desired. // var context = sender as ObjectContext; // if (context == null) // return; // foreach (ObjectStateEntry item in // context.ObjectStateManager.GetObjectStateEntries( // EntityState.Modified | EntityState.Added)) // { // // Do something important here // if ((item.Entity as Inventory) != null) // { // var entity = (Inventory)item.Entity; // if (entity.Color == "Red") // { // item.RejectPropertyChanges(nameof(entity.Color)); // } // } // } //} //protected override void Dispose(bool disposing) //{ // DbInterception.Remove(databaseLogger); // databaseLogger.StopLogging(); // base.Dispose(disposing); //} public virtual DbSet<CreditRisk> CreditRisks { get; set; } public virtual DbSet<Customer> Customers { get; set; } public virtual DbSet<Inventory> Inventory { get; set; } public virtual DbSet<Order> Orders { get; set; } } } 

EF 6.1不支持使用EF 5 Chares的脚手架模板