The answers 1 to 3 are good.
For the case shown in the question, since the
Name
is required to be inserted in the
chkInOut
record in the
UpdateTime
method, I think the following statements can be used in the
else
clause of
UpdateTime
method.
protected void UpdateTime(int EmpId)
{
if (chkInOut == null)
{
}
else
{
chkInOut.CheckOutTime = DateTime.Now;
var employee = db.Employees.FirstOrDefault ( emp => emp.Id == chkInOut.EmpId);
chkInOut.Name = employee == null ? string.Empty : employee.Name;
db.SubmitChanges();
}
}