C# Appharbor上的多个活动结果集(MARS)如何启用?

C# Appharbor上的多个活动结果集(MARS)如何启用?,c#,asp.net-mvc-3,entity-framework,appharbor,sql-server-mars,C#,Asp.net Mvc 3,Entity Framework,Appharbor,Sql Server Mars,这是我在stackoverflow的第一个问题,请耐心听我说 在我的本地项目中,我使用ASP.NET MVC 3和实体框架4.3以及SQL Express 在我的连接字符串中,我将MARS设置为true 但当我将项目部署到Appharbor时,Appharbor会注入自己的连接字符串 连接字符串中未设置MARS的位置。所以我得到这个: There is already an open DataReader associated with this Command which must be cl

这是我在stackoverflow的第一个问题,请耐心听我说

在我的本地项目中,我使用ASP.NET MVC 3和实体框架4.3以及SQL Express

在我的连接字符串中,我将MARS设置为true

但当我将项目部署到Appharbor时,Appharbor会注入自己的连接字符串

连接字符串中未设置MARS的位置。所以我得到这个:

There is already an open DataReader associated with this Command which must be closed first.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. 
到目前为止,我在这方面看到的最佳解决方案是:

如果他们有此代码:

var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var connectionString = configuration.ConnectionStrings.ConnectionStrings["ConnectionstringAlias"].ConnectionString;
if (!connectionString.Contains("MultipleActiveResultSets=True;"))
{
    connectionString += "MultipleActiveResultSets=True;";
}

configuration.ConnectionStrings.ConnectionStrings["ConnectionstringAlias"].ConnectionString = connectionString;
configuration.Save();
我想不出该把它放在哪里,我已经试着把它放在全球范围内

应用程序启动()方法

但这给了我一个错误:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 50: 
Line 51:             var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
Line 52:             var connectionString = configuration.ConnectionStrings.ConnectionStrings["ConnectionstringAlias"].ConnectionString;
Line 53:             if (!connectionString.Contains("MultipleActiveResultSets=True;"))
Line 54:             {

Source File: C:\Users\William\Documents\Visual Studio 2010\Projects\xxx\xxx\Global.asax.cs    Line: 52 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   Educationexpander.MvcApplication.Application_Start() in C:\Users\William\Documents\Visual Studio 2010\Projects\xxx\xxx\Global.asax.cs:52
是否有人可以解决此问题?

更新:现在可以使用Sequelizer管理面板为注入的连接字符串启用多个活动结果集(MARS)。这是推荐的方法,因为不再需要修改
web.config
,这会导致启动期间重新加载
AppDomain

答案就在上面的评论中,这只是形式化而已


您需要使用正确的
ConnectionstringAlias
(感谢@chris sainty),并且您必须在应用程序设置中启用文件系统写入,以便将更新的配置写入磁盘。

在AppHarbor的代码段中,您是否用自己的连接字符串名称替换了“ConnectionstringAlias”?您已经正确地放置了代码,但是应该将最后两行移到if语句中。演示如何创建上下文。您需要以“name={ConnectionStringName}”的形式提供连接字符串的名称,就像它在配置文件中一样,或者您需要提供连接字符串作为参数。@chrissaint谢谢您的回答我尝试了您的解决方案,但它给了我以下错误:“/”应用程序中的服务器错误。配置错误说明:在处理为该请求提供服务所需的配置文件时出错。请查看下面的特定错误详细信息,并适当修改配置文件。分析器错误消息:加载配置文件时出错:对路径“D:\websites\96\24c56\0x0049\U PublishedWebsites\Myproject\jwmgj11b.tmp”的访问被拒绝。源代码错误:[没有相关的源代码行]但这在我的本地版本上可以正常工作。那么,我猜这是appharbor的问题?@WilliamHJacobsen如果您正在向实例文件系统写入,您需要在应用程序设置中启用该功能。我在实现这一点时遇到了一些困难,我想我会分享我添加到
App\u Start()
中的(有点像雪花一样)内容。代码示例将我的连接字符串的格式设置为
MultipleActiveResets=true添加在结束报价之后。由于web.config update->save->restart->save->restart等原因,每次保存连接字符串都会将我的应用程序发送到一个无限循环中,因此我只在需要更新时保存它。