Sequence包含多个元素错误,但未保留在数据库

时间:2015-06-19 13:22:35

标签: c# linq

我已经探索了一个非常特别的事情:如果我运行我的应用程序,我将获得正在使用我的应用程序的当前客户。

首先,控制器中的代码正在运行,然后是服务中的代码,最后但并非最不重要的是后端的代码。

如果我在后端代码中设置断点并运行我的应用程序,后端的代码永远不会到达我设置断点的代码。

但是,这很疯狂,我收到状态码错误500(内部服务器错误)。当我在浏览器谷歌浏览器中查找此错误时,我看到了这一点:

ExceptionMessage: "Sequence contains more than one element"
ExceptionType: "System.InvalidOperationException"
Message: "An error has occurred."
StackTrace: " at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
↵ at Microsoft.Owin.Security.AuthenticationManager.<AuthenticateAsync>d__8.MoveNext()
↵--- End of stack trace from previous location where exception was thrown ---
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵ at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
↵ at System.Web.Http.HostAuthenticationFilter.<AuthenticateAsync>d__0.MoveNext()
↵--- End of stack trace from previous location where exception was thrown ---
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵ at    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
↵ at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()
↵--- End of stack trace from previous location where exception was thrown ---
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
↵ at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"

所以我已经检查了该错误发生了什么。我读到的只是关于&#34; SingleOrDefault&#34;或者&#34; FirstOrDefault&#34;。我有FirstOrDefault,但是没有循环通过我设置的断点,所以它如何知道序列包含多个元素?我理解错误吗?

控制器:

CustomerService.current().then(function(customer) {
    ...
}

服务

.factory('CustomerService', function(Restangular) {
  return {
     current: function() {
        return Restangular.one('api/currentCustomer').get();
     },
  };

})

后端

   [Route("api/currentCustomer")]      
   [Authorize(Roles = "User")]
   public IHttpActionResult GetCurrentCustomer()
   {
      IHttpActionResult result;

      try
      {             
         Customer customer = repository.GetCurrentCustomer(HttpContext.Current.User.Identity.Name);
         if (customer != null)
         {
            CustomerViewModel customerViewModel = new CustomerViewModel(customer);
            result = Ok(customerViewModel);
         }
         else
         {                
            //throw exception
         }
      }
      catch (Exception exception)
      {            
         //throw Exception
      }       
      return result;
   }


public Customer GetCurrentCustomer(string name)
  {
     using (IResModelContainer entities = new IResModelContainer())
     {
        Customer customer = entities.Customers.FirstOrDefault(c => c.UserLogin.Equals(name));
        return customer;
     }
  }

0 个答案:

没有答案