Entity Framework - refresh objects from database

Posted by Nebo on Stack Overflow See other posts from Stack Overflow or by Nebo
Published on 2010-04-01T23:29:41Z Indexed on 2010/04/01 23:33 UTC
Read the original article Hit count: 316

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

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about database