9

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?

3
  • Try to set a break point to take a look if your query variable carry any values? Commented Sep 19, 2013 at 4:07
  • 7
    Avoid throw ex; as the property way to re-throw an exception is throw;. 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. Commented Sep 19, 2013 at 4:13
  • 2
    I would avoid naming your variable query since it most obviously is not a query (the bit inside the brackets is a query, but the variable is assigned the result of FirstOrDefault()). It is instead a site. Also why are you using FirstOrDefault() if you are not checking for the default (null)? Why not just use First() since you'll end up with an exception either way given the current code. Commented Sep 19, 2013 at 5:01

3 Answers 3

18

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe this is now the preferred usage (with Entry) in EF6.
This is not "preferred usage". OP's code should work as is. The question lacks info to diagnose why it doesn't.
5

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

1 Comment

I have done the same, still getting error "The specified network name is no longer available.", Select work fine, this exception is occurring while savechange()
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.