0

现在大约一两天一次,当人们尝试登录时,我的网站开始抛出错误,并且在我重新启动 IIS 之前没有人可以登录。Any() 怎么能做到这一点,我能做些什么来解决它?

这是抛出异常的函数:

        public override bool ValidateUser(string username, string password)
    {
        var db = Access.Context;
        var query =
            from p in db.Players
            where p.PlayerName == username
                && p.Password == password 
            select p
        ;
        return query.Any();
    }

其他 LINQ to SQL 查询工作正常。这是堆栈跟踪。

InvalidOperationException: Sequence contains no elements 

   System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +1151
    System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113
    System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +344
    System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +23
    System.Linq.Queryable.Any(IQueryable`1 source) +243
    FortyTwoAccess.PlayerProvider.ValidateUser(String username, String password) in C:\dev\FortyTwo\FortyTwoAccess\PlayerProvider.cs:114
    FortyTwoClient.Models.AccountMembershipService.ValidateUser(String userName, String password) in C:\dev\FortyTwo\FortyTwoClient\Models\AccountModels.cs:117
    FortyTwoClient.Controllers.AccountController.LogOn(LogOnModel model, String returnUrl) in C:\dev\FortyTwo\FortyTwoClient\Controllers\AccountController.cs:42
    lambda_method(Closure , ControllerBase , Object[] ) +157
    System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
4

2 回答 2

0

这行得通吗?

public override bool ValidateUser(string username, string password)
{
    var db = Access.Context;
    return db.Players.Any(p => p.PlayerName == username && p.Password == password);
}
于 2013-01-08T13:36:54.453 回答
0

对此的解决方法是不使用类变量来保存上下文,而是在使用 using 包装的每个方法中本地实例化上下文。

于 2013-01-30T04:39:57.113 回答