Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 序列不包含服务器端的元素_C#_Asp.net Mvc_Database_Linq - Fatal编程技术网

C# 序列不包含服务器端的元素

C# 序列不包含服务器端的元素,c#,asp.net-mvc,database,linq,C#,Asp.net Mvc,Database,Linq,我正在开发一个Asp.net MVC web应用程序,并且已经在web服务器上部署了几个月了。就在最近,我经历了一个让我困惑的新错误。发布应用程序并加载网站后,我收到以下错误消息: Sequence contains no elements Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace fo

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

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

当您收到LINQ错误“序列不包含元素”时,这是 通常是因为您使用的是First()或Single()命令,而不是 而不是FirstOrDefault()和SingleOrDefault()


这个异常在代码中的什么地方抛出,您能展示一些代码吗?这是我一直坚持的部分。通过localhost查看网站时,不会在任何位置引发此错误。一旦我通过发布的服务器查看它,它就会在每个页面上抛出。这个错误是在应用程序的登录页/主页上出现的。如果我没记错的话,您正在那里查询数据库,您没有检查返回的集合中是否有任何元素,您正在使用它。非常感谢!我彻底检查了Global.asax.cs文件中的一行代码,该代码在每次请求时都会被调用,从而导致空值。