Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - refresh objects from database

I'm having trouble with refreshing objects in my database. I have an two PC's and two applications.

On the first PC, there's an application which communicates with my database and adds some data to Measurements table. On my other PC, there's an application which retrives the latest Measurement under a timer, so it should retrive measurements added by the application on my first PC too.

The problem is it doesn't. On my application start, it caches all the data from database and never get new data added. I use Refresh() method which works well when I change any of the cached data, but it doesn't refresh newly added data.

Here is my method which should update the data:

    public static Entities myEntities = new Entities();

    public static Measurement GetLastMeasurement(int conditionId)
    {
        myEntities.Refresh(RefreshMode.StoreWins, myEntities.Measurements);

        return (from measurement in myEntities.Measurements
                where measurement.ConditionId == conditionId
                select measurement).OrderByDescending(cd => cd.Timestamp).First();
    }

P.S. Applications have different connection strings in app.config (different accounts for the same DB).

like image 247
Nebojsa Veron Avatar asked Apr 01 '10 23:04

Nebojsa Veron


People also ask

How to refresh an entity in a context?

The best way to refresh entities in your context is to dispose your context and create a new one. If you really need to refresh some entity and you are using Code First approach with DbContext class, you can use.

How do I update an object in Entity Framework?

Updating Entity Framework Objects with Changed Data Assuming you're using the latest version of Entity Framework, the easiest way to update your database is to use DbContext's Entry class: It's just two lines of code no matter how many properties your object has.

How to reload a specific entity in Entity Framework?

If you want to reload specific entities, with the DbContextApi, RX_DID_RX already gave you the answer. If you are using Entity Framework 4.1+ (EF5, or EF 6 probably), DbContext API: public void RefreshAll () { foreach (var entity in ctx.ChangeTracker.Entries ()) { entity.Reload (); } }

What is the purpose of the object context refresh method?

This method has the dual purpose of allowing objects in the object context to be refreshed with data from the data source, and being the mechanism by which conflicts can be resolved. For more information, see Saving Changes and Managing Concurrency. The order in which objects are refreshed is nondeterministic.


2 Answers

This should work:

public static Entities myEntities = new Entities();

public static Measurement GetLastMeasurement(int conditionId)
{
    myEntities.Refresh(RefreshMode.StoreWins, myEntities.Measurements);
    var allMeasurements = myEntities.Measurements.ToList();//retrieves all measurements from database

    return (from measurement in allMeasurements
            where measurement.ConditionId == conditionId
            select measurement).OrderByDescending(cd => cd.Timestamp).First();
}

What sense makes caching when you refresh store every time you want to use it? You could chage it to:

public Measurement GetLastMeasurement(int conditionId)
{
    var entities = new Entities();
    return (from measurement in entities.Measurements
            where measurement.ConditionId == conditionId
            select measurement).OrderByDescending(cd => cd.Timestamp).First();
}

It also look up in database with every call, but makes much less operations.

like image 132
LukLed Avatar answered Oct 02 '22 01:10

LukLed


As of EF 4.1 you can use AsNoTracking() method on your entities.

return myEntities.Measurements.AsNoTracking();

Note that AsNoTracking() will not add the entities to your context for tracking, but merely return them fresh from your data store.

For more info see http://blogs.msdn.com/b/adonet/archive/2011/02/05/using-dbcontext-in-ef-feature-ctp5-part-11-load-and-asnotracking.aspx

like image 26
Rei Mavronicolas Avatar answered Oct 02 '22 02:10

Rei Mavronicolas