这是我在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();
我不知道该放在哪里,试图将其放在全球范围内
Aplication_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
有没有人可以解决这个问题?
最佳答案
更新:现在可以使用Sequelizer管理面板为注入的连接字符串启用多个活动结果集(MARS)。这是推荐的方法,因为web.config
不再需要修改,这会导致在启动过程中重新加载AppDomain
答案在上面的评论中,这只是形式化。
您需要使用正确的ConnectionstringAlias
(感谢@ chris-sainty),并且必须在应用程序设置中启用文件系统写操作,才能将更新后的配置写入磁盘。