chamara
chamara

Reputation: 12711

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?

Upvotes: 9

Views: 29565

Answers (3)

Ernest
Ernest

Reputation: 2209

Also noticed this happening when the Entity doesn't have configured a primary Key. On EF Core check your OnModelCreating and make sure entity.HasKey(e => e.TheKey); is present.

By the documentation you are not required to work with states: https://learn.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli

var yourEntity = await context.FirstOrDefaultAsync(e => e.Id = 1);//You can also call FindAsync(id)
yourEntity.Name = "NewName";//This line will let EF Core knows the entity has been modify
await context.SaveChangesAsync();

Although you can check how to work with states so here: https://learn.microsoft.com/en-us/ef/ef6/saving/change-tracking/entity-state And the code will look like:

var yourEntity = await context.FirstOrDefaultAsync(e => e.Id = 1);
yourEntity.Name = "NewName";
context.Entry(yourEntity).State = EntityState.Modified;
await context.SaveChangesAsync();

They are both the same, I honestly prefer manually setting the state (in big projects) sometimes EF Core loses tracking of some entities (this is a personal experience) so I double check updating the status as Added Or Modified. Thanks

Upvotes: 0

Aleksei Chepovoi
Aleksei Chepovoi

Reputation: 3955

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.

Upvotes: 18

Milad Hosseinpanahi
Milad Hosseinpanahi

Reputation: 1495

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();
            }

Upvotes: 5

Related Questions