Entity Framework update record not working

asp.net-mvc asp.net-mvc-4 entity-framework linq

Question

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?

1
8
9/19/2013 4:03:22 AM

Popular Answer

After You modify query object You should change it's state to Modified before calling context.SaveChanges(). Your context object should know about the entity that You modify. Assuming dc is Your context object:

query.CalledInAt = gate.CalledInAt;
//change other properties ..
dc.Entry(query).State = EntityState.Modified;
dc.SaveChanges();

That should work for You.

11
9/19/2013 8:51:47 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow