【问题标题】:Updating Only a Few Properties in a Context - EF4 Sequence Contains No Elements Error仅更新上下文中的几个属性 - EF4 序列不包含元素错误
【发布时间】:2013-07-08 20:59:09
【问题描述】:

我正在尝试更新单个记录中的 2 个字段(评论和 DateFolluwUpResponse),但是当我这样做时,我得到了一个异常:

Sequence contains no elements

我正在遵循 Julia Lerman 的 DbContext 书中“更改现有实体”部分中的示例

[HttpPost]
public ActionResult FollowUp(FollowUpViewModel m)
{

   //Update record in Prospects

    int recordIdToUpdate = m.Prospect.id;
    string followUpComments = (!String.IsNullOrEmpty(m.Prospect.Comments) ? m.Prospect.Comments : String.Empty);
    DateTime followUpDate = DateTime.Now;

    //Update record
    using (var context = new LocatorContext()){

        //---------------------------------------
        //Exception happens here

        var followUpUpdate = (from p in context.Prospects where p.id == recordIdToUpdate select p).Single();

       //----------------------------------------

        followUpUpdate.Comments = followUpComments;
        followUpUpdate.DateFollowUpResponse = followUpDate;

        context.SaveChanges();

    }


    return View();
}

【问题讨论】:

    标签: asp.net-mvc-3 entity-framework-4


    【解决方案1】:

    方法Single 抛出异常,因为查询没有返回值。

    【讨论】:

      猜你喜欢
      • 2019-02-04
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多