c# - 序列不包含任何元素服务器端

标签 c# asp.net-mvc database linq

我正在开发一个 Asp.net MVC 网络应用程序,并且已经部署到网络服务器几个月了。就在最近,我遇到了一个让我困惑的新错误。在发布我的应用程序并加载网站后,我收到以下错误消息:

 Sequence contains no elements

 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: Sequence contains no elements

 Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[InvalidOperationException: Sequence contains no elements]
 System.Linq.Enumerable.First(IEnumerable`1 source) +514
System.Linq.Queryable.First(IQueryable`1 source) +330
LabSys.MvcApplication.Application_AuthorizeRequest(Object sender, EventArgs   e) +1261
  System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&completedSynchronously) +165

我很困惑,因为应用程序在本地主机上运行完美,没有任何问题或错误消息。我还检查了我的数据库,它也已连接并验证到服务器。所以应该不会出错。

提前感谢您的帮助!

最佳答案

检查您的代码以确保使用 FirstOrDefault()SingleOrDefault() 而不是使用以下任何一个:First()Single()

When you get theLINQ Error "Sequence contains no elements", this is usually because you are using the First() or Single() command rather than FirstOrDefault() and SingleOrDefault().

Sequence contains no elements?

关于c# - 序列不包含任何元素服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30945692/

相关文章:

php - Laravel 加入,包括没有匹配的地方

c# - 检查两个对象的属性以进行更改

c# - 获取和处理 MVC 中输入的数据 - 简单

MySql 计算

c# - 如何将 PDF 保存为只读/扁平化?

asp.net-mvc - 当我使用 mvc 在 https 中发布表单时,密码可见

javascript - IndexedDB 在需要升级时处理数据迁移

c# - 动态初始化 iset<t>

c# - 锁定困惑的串口端口句柄

c# - 获取 User.identity 的名字和姓氏