Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework update record not working

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?

like image 541
chamara Avatar asked Sep 19 '13 04:09

chamara


People also ask

How do I update Entity Framework records?

We can update records either in connected or disconnected scenarios. In the connected Scenario, we open the context, query for the entity, edit it, and call the SaveChanges method. In the Disconnected scenario, we already have the entity with use. Hence all we need to is to attach/add it to the context.

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

How do I update my Entity Framework model in .NET core?

Right-click anywhere on the design surface, and select Update Model from Database... In the Update Wizard, select the Refresh tab and select your table then click Finish button.


2 Answers

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.

like image 167
Aleksei Chepovoi Avatar answered Sep 19 '22 19:09

Aleksei Chepovoi


You have to use the entityframework to select your object, with that the result object will be track-able, so try this

            using (var dc = new GateEntities())
            {
                var gate = dc.tblSites.Where(g => g.ID == date.ID).FirstOrDefault();

                gate.CalledInAt = gate.CalledInAt;
                gate.CallerRequest = gate.CallerRequest;
                gate.ContactPersonOnSite = gate.ContactPersonOnSite;
                gate.Email = gate.Email;
                gate.EmailSitePerson = gate.EmailSitePerson;

                dc.SaveChanges();
            }
like image 41
Milad Hosseinpanahi Avatar answered Sep 18 '22 19:09

Milad Hosseinpanahi