none
EF5 - Refresh single outdated entity w/o cloning manually RRS feed

  • Question

  • Seems I'm not finding proper web-search keywords for the following:

    I want to refresh an existing, outdated entity object and doing ...

    public void Refresh(Customer outdatedEntity)
    {
        using (SomeEntities db = new SomeEntities())
        {
            Customer currentEntity = db.Customers.Find(outdatedEntity.id);
            if (currentEntity != null)
            {
                // ...works well so far, the entity is being found -
                // now I could easily update the Entry() item by taking over from the outdatedEntity:
                db.Entry(currentEntity).CurrentValues.SetValues(outdatedEntity);
    
                // But I want to do it just the other way around, i.e. update the parameter with current
                // values in a simplified way.
                // 
                // IOW, I'm hoping there might be something built into Entity Framework 5 so that I
                // do not need to write an own foreach() loop in order to update the properties one by one?
            }
        }
    }
    
    

    Thanks in advance

    -Stefan

    Sunday, January 6, 2013 8:58 PM

Answers

  • Hi Stefan,

    Please check if this is what you want:

                    foreach (var trackedEntity in context.ChangeTracker.Entries())
                    {
                        if (trackedEntity.State == System.Data.EntityState.Unchanged || trackedEntity.State != System.Data.EntityState.Modified)//You can set the corresponding state you wan to filter.
                        {
                            trackedEntity.GetDatabaseValues();//Refresh to the current database values.
                        }
                    }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stefan Wuebbe Sunday, January 13, 2013 12:23 PM
    Friday, January 11, 2013 9:36 AM

All replies

  • Hi Stefan,

    Welcome to the MSDN forum.

    I am not sure whether you mean there is a record in database has the same ID and you want to update this outdated entity object. If I understand correctly, If the entity object has not been tracked, you can use this approach to update.:

        using (SomeEntities db = new SomeEntities()) 
        { 
            db.Customers.Add(outdatedEntity);
            db.Entry(outdatedEntity).State = System.Data.EntityState.Modified;
            db.SaveChanges();
        }

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, January 8, 2013 5:57 AM
  • Hi Alexander,

    My question was not well written, sorry!, hope this reworded attempt works better:
    What I meant was that a local client instance has retrieved some entity objects first.
    Then, later, the database is getting updated by another instance so that the local content in the first instance is not up-to-date anymore.
    So the "existing", local object is outdated and wants to get renewed.

    In other words, I'm looking for the most appropriate "best practice" way to refresh outdated local content,
    and assumed it would be easier to update the local object reference rather than removing the outdated object from a C# List<> and then adding the new one, checking whether the old one had been a ViewModel.CurrentItem before or not etc. - perhaps I'm on the wrong track though...

    Thanks again!
    -Stefan

    Tuesday, January 8, 2013 10:45 PM
  • Hi Stefan,

    Please check if this is what you want:

                    foreach (var trackedEntity in context.ChangeTracker.Entries())
                    {
                        if (trackedEntity.State == System.Data.EntityState.Unchanged || trackedEntity.State != System.Data.EntityState.Modified)//You can set the corresponding state you wan to filter.
                        {
                            trackedEntity.GetDatabaseValues();//Refresh to the current database values.
                        }
                    }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stefan Wuebbe Sunday, January 13, 2013 12:23 PM
    Friday, January 11, 2013 9:36 AM
  • Hey, that looks very promising,  thank you very much! - will try this weekend, and then also "mark as answer" of course < s >

    Later: as far as I can see, the implicit answer to my original question is "No, there is nothing built-in" so far.
    If so, that's no problem because creating the required code to get property values from one (newer) entity object and apply them to the "existing" outdated object is easy to do.

    The context.ChangeTracker example you posted looks like the answer to a question in the same context that I had not asked yet but probably would have later, Thanks again!


    Friday, January 11, 2013 10:26 AM