3

这是我在 stackoverflow 上的第一个问题,请耐心等待。

在我的本地项目中,我将 ASP.NET MVC 3 和 Entity Framework 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. 

到目前为止,我在这方面看到的最佳解决方案:http: //support.appharbor.com/kb/add-ons/using-sequelizer

他们在哪里有这个代码:

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();

我不知道把它放在哪里,试图把它放在全局下

Application_Start() 方法

但这给了我这个错误:

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

有没有人可以解决这个问题?

4

1 回答 1

3

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

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

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

于 2012-05-22T20:53:11.760 回答