仅更新上下文中的少数属性 - EF4序列不包含任何元素错误

时间:2013-07-08 12:47:50

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

我正在尝试在单个记录中更新2个字段(Comments和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();
}

1 个答案:

答案 0 :(得分:1)

方法Single抛出异常,因为查询不返回任何值。