Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework POCO SaveChanges() on Update doesn't work?

I'm working with EF CTP 4, using POCO models, adding a new object and call SaveChanges() works but updating the object doesn't work. Here's the code for update:

public void UpdateContact(Contact contact)
        {
            var findContact = GetContact(contact.ContactID);
            findContact = contact;
            _context.SaveChanges();
        }

public Contact GetContact(int contactId)
        {
            return GetAllContacts().SingleOrDefault(c => c.ContactID == contactId);
        }

public IQueryable<Contact> GetAllContacts()
        {
            return _context.Contacts;
        }

I'm not sure what I'm doing wrong here. Any idea? Thanks.

like image 838
Saxman Avatar asked Nov 02 '10 05:11

Saxman


People also ask

How do I Save changes in Entity Framework Core?

Entity Framework Core Save Changes to the database using the SaveChanges method of DbContext. When we use the SaveChanges it prepares the corresponding insert, update, delete queries. It then wraps them in a Transaction and sends it to the database.

Why does savechanges not work when context is closed?

It rollbacks all the changes if any of the statement fails. But for the SaveChanges to work, the Context must correctly mark the entity as Updated, Added or Deleted etc. But if we close the context and make changes to the entity, those will not be tracked by the context. Hence we have two possibilities here.

How does the savechanges method work in dbcontext?

The SaveChanges method of the DbContext prepares the Insert, Update & Delete Queries. It does so by tracking the changes to each of the entities’ context is tracking. Whenever we query the database for entities, the context retrieves them and mark the entity as Unchanged. The ChangeTracker property of the DbContext keeps track of the entities.

How do I get a modified entity into context?

Use Attach to get your modified entity into context .Before you save. Context had no idea what you are talking about. That's because you need to pass the paymentAttempt object to your context, to let it know that it is an object that needs to be updated. For example, assuming that _auctionContext is an instance of DbContext:


1 Answers

The problem is that when you assign findContact = contact the EntityState does not get changed in the ObjectStateManager (so it's still set to Unchanged). Therefore, no Update SQL statement gets generated for the entity. You have several options to do the update:

Option 1: Populate findContact field-by-field:

var findContact = GetContact(contact.ContactID);
findContact.FirstName = contact.FirstName;
findContact.LastName = contact.LastName;
...
_context.SaveChanges();

Option 2: Use the ApplyCurrentValues method:

var findContact = GetContact(contact.ContactID);
_context.ApplyCurrentValues("Contacts", contact);
_context.SaveChanges();

Option 3: Attach the updated entity and change the state in the ObjectStateManager. (Note: this will not make a roundtrip to the database for fetching the existing contact).

_context.AttachTo("Contacts", contact);
var contactEntry = Context.ObjectStateManager.GetObjectStateEntry(contact);
contactEntry.ChangeState(EntityState.Modified);
_context.SaveChanges();
like image 55
Yakimych Avatar answered Sep 20 '22 15:09

Yakimych