Entity Framework - refresh objects from database

╄→尐↘猪︶ㄣ 提交于 2019-12-04 18:13:20

问题


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


回答1:


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.




回答2:


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




回答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



来源:https://stackoverflow.com/questions/2564265/entity-framework-refresh-objects-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!