Entity Framework - refresh objects from database

前端 未结 3 848
北荒
北荒 2021-02-14 09:53

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

3条回答
  •  礼貌的吻别
    2021-02-14 10:49

    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.

提交回复
热议问题