0

我有一个网站,有时会失去对数据库的访问权限。该网站仍然在 IIS 上运行,我可以毫无问题地访问首页。

我在开发或测试中没有遇到这个错误,只有在现场。

我第一个记录的异常似乎总是一个 InvalidCastException,带有以下堆栈跟踪。

06-03-2013 09:06:26 
The exception was: Specified cast is not valid.
at System.Data.SqlClient.SqlBuffer.get_Int32()
at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
at Read_Question(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Linq.EntityRef`1.get_Entity()
at DB.TempQuestion.get_Question() in xxxx\DBLinq.designer.cs:line 9808
at xxxx.TestController.BuildBarHelperForPersonAssignment(PersonAssignment pa) in xxxx\TestController.cs:line 751
at xxxx.TestController.BuildMovieModel(TempMovie tm, PersonAssignment pa) in xxxx\TestController.cs:line 792
at xxxx.TestController.ShowMovie(TempMovie tm, PersonAssignment pa) in xxxxx\TestController.cs:line 636
at xxxx.TestController.Index(String id, FormCollection form, Nullable`1 currentid, String type, Int32[] selectedTest) in xxxx\TestController.cs:line 313

在此之后,我收到各种 InvalidCastExceptions 和“服务器无法恢复事务”错误。

很清楚错误发生在哪里。我的问题是为什么会发生这种情况以及为什么会导致应用程序无法访问数据库?

我认为错误出现在所有这些之前。

我的应用程序设置:

ASP.NET MVC2、LINQ2SQL、MsSQL Server 2008 R2、IIS 7.5 (ASP 2.0)

我的控制器设置:

我担心我需要重新设计我的控制器,它们几乎都是这样的。

[Authorize(Roles = "xxxxx")]
Public class ABController : Controller
{
    private readonly DBLinqDataContext _db = new DBLinqDataContext();

    public ActionResult Index()
    {
        ...
    }
}

由于未关闭的 DBLinqDataContext 会发生此错误吗?

添加以下覆盖的方法可能有帮助吗?

    protected override void Dispose(bool disposing)
    {
        _db.Dispose();
        base.Dispose(disposing);
    }

如果您需要更多信息,请与我们联系!

4

2 回答 2

0

看起来您已将其中一个TempQuestion属性类型从int更改为某物,但忘记更改相应表列的类型。

于 2013-03-06T19:30:58.390 回答
0

解决了...

这是由于多个未处理的dataContext

于 2013-03-07T22:09:41.713 回答