我正在尝试更新表中的单个记录,但是当我运行时.Firstordefault(),我得到错误:"对象引用未设置为对象的实例.",如果使用它.First(),我得到"序列不包含任何元素".
使用它的另一个地方,它的工作正常,但这次它导致错误.
这是代码:
public class AllownceDetails
{
public int ta_id{get;set;}
public int tvrid{get;set;}
public DateTime ofDate{get;set;}
public string status{get;set;}
public string userid {get;set;}
}
//Update Method
public void Update(AllownceDetails Allowncedtl)
{
var ta = (from a in ce.tbl_tvrallownce
where a.tvrid == Allowncedtl.tvrid
//error: Sequence contains no elements
select a).SingleOrDefault();
ta.status = Allowncedtl.status;
//error:Object reference not set to an instance of an object
ce.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)