简体   繁体   English

序列在Entity Framework中不包含任何记录

[英]Sequence contains no records in Entity Framework

StackTrace = "   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)\r\n   at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__0[TResult](IEnumerable`1 sequence)\r\n   at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult]...

It was working before all of a sudden not able to get any records from the table using entity framework 在使用实体框架突然无法从表中获取任何记录之前,它一直在工作

Don't know what is the issue it worked fine before haven't made any changes to the edmx file either 在未对edmx文件进行任何更改之前,不知道它能正常工作的问题是什么

  public bool SetClientIdForUser(string username, string clientId)
    {
        aspnet_Users aspnetUsers = _objVaccinationContext.aspnet_Users.First(t => t.UserName == username);
        //aspnetUsers.Client_Id = clientId;
        aspnetUsers.Client_id = clientId;
        var entry = _objVaccinationContext.Entry(aspnetUsers);
        entry.State = EntityState.Modified;
        _objVaccinationContext.SaveChanges();
        return true;
    }

在此处输入图片说明

CONNECTION STRING : <add name="ChildVaccinationEntities" connectionString="metadata=res://*/ChildVaccinationContext.ChildVaccination.csdl|res://*/ChildVaccinationContext.ChildVaccination.ssdl|res://*/ChildVaccinationContext.ChildVaccination.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=CVSUAT;user id=sa;password=Password123;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 连接字符串: <add name="ChildVaccinationEntities" connectionString="metadata=res://*/ChildVaccinationContext.ChildVaccination.csdl|res://*/ChildVaccinationContext.ChildVaccination.ssdl|res://*/ChildVaccinationContext.ChildVaccination.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=CVSUAT;user id=sa;password=Password123;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> <add name="ChildVaccinationEntities" connectionString="metadata=res://*/ChildVaccinationContext.ChildVaccination.csdl|res://*/ChildVaccinationContext.ChildVaccination.ssdl|res://*/ChildVaccinationContext.ChildVaccination.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=CVSUAT;user id=sa;password=Password123;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

NOTE: the first record in my table is the record i want to get so cannot use any other function 注意:我表中的第一条记录是我想要获取的记录,因此无法使用任何其他功能

You can use FirstOrDefault then check for null 您可以使用FirstOrDefault然后检查null

aspnet_Users aspnetUsers = _objVaccinationContext.aspnet_Users.FirstOrDefault(t => t.UserName == username);

if(aspnetUsers != null)
{

  //do your stuff
return true
}

else
{
return false
}

The First method of LINQ throws an exception if the filtered sequence does not at least contain one element. 如果过滤后的序列至少不包含一个元素,则LINQ的First方法将引发异常。 So if the username is not found within your database, this line of code throws an error. 因此,如果在数据库中找不到username ,则此行代码将引发错误。 You can use FirstOrDefault and check for null . 您可以使用FirstOrDefault并检查是否为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM