I'm new to Entity Framework. I'm trying to update a record and save changes to the database.
public void SaveEdit(Gate gate)
{
try
{
using (dc = new GateEntities())
{
var query = (from tbsite in dc.tblSites
where tbsite.ID == gate.ID
select tbsite).FirstOrDefault();
query.CalledInAt = gate.CalledInAt;
query.CallerRequest = gate.CallerRequest;
query.ContactPersonOnSite = gate.ContactPersonOnSite;
query.Email = gate.Email;
query.EmailSitePerson = gate.EmailSitePerson;
dc.SaveChanges();
}
}
catch (Exception ex)
{
throw ex;
}
}
It gets no exceptions or error messages but it does not save the changes to the database. why it's not updating the record?
queryvariable carry any values?throw ex;as the property way to re-throw an exception isthrow;. If an exception is re-thrown by specifying the exception in the throw statement, the stack trace is restarted at the current method and the list of method calls between the original method that threw the exception and the current method is lost. To keep the original stack trace information with the exception, use the throw statement without specifying the exception.querysince it most obviously is not a query (the bit inside the brackets is a query, but the variable is assigned the result ofFirstOrDefault()). It is instead a site. Also why are you usingFirstOrDefault()if you are not checking for the default (null)? Why not just useFirst()since you'll end up with an exception either way given the current code.