Entity Framework - refresh objects from database
Asked Answered
C

3

10

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).

Connected answered 1/4, 2010 at 23:29 Comment(1)
You can check my answer here https://mcmap.net/q/770123/-objectcontext-refreshFillin
W
9

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.

Winterwinterbottom answered 2/4, 2010 at 9:4 Comment(4)
First solution you mentioned still doesn't retrive newly added data from my database. Your second solution I expected to work, doesn't work either. I wouldn't prefer it because this way I open a new context on my database every time I want to get the last measurement. When I restart my application it retrives new data, but it doesn't retrive if there's something added while application is working. Is there a possibilty that different accounts I'm using for each application (one that adds and one that retrives data from DB) are the reason for this?Connected
With any of those two solutions I get the same like I already had. If I edit a row in a Measurements table (which has been retrived on the application start) it updates it. But if I add a new row, it won't retrive it. In both cases. This time I edited an existing and added new row directly using my SQL Manangement Studio.Connected
@Nebo: Are you sure you do it right? Do you have SQL Profiler? Can you look at executed SQL?Winterwinterbottom
Hmm, there's a weird problem I have. Actually both solutions work well, it's just my binding doesn't get updated. I'll have to figure that out. Thank you!Connected
E
8

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

Evanesce answered 8/6, 2012 at 10:26 Comment(0)
J
3

Another possibility is to use MergeOption to decide how you want to manage the objects in the context. For example MergeOption.OverwriteChanges will overwrite the object context with values from the data source.

For more info see http://msdn.microsoft.com/en-us/library/system.data.objects.mergeoption.aspx

Jed answered 12/8, 2012 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.