database 实体框架 - 从数据库刷新对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2564265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:40:22  来源:igfitidea点击:

Entity Framework - refresh objects from database

wpfdatabaseentity-frameworkobjectcontext

提问by Nebojsa Veron

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

我在刷新数据库中的对象时遇到问题。我有两台 PC 和两个应用程序。

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.

在第一台 PC 上,有一个应用程序与我的数据库进行通信并将一些数据添加到测量表中。在我的另一台 PC 上,有一个应用程序可以在计时器下检索最新的测量值,因此它也应该在我的第一台 PC 上检索应用程序添加的测量值。

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.

问题是它没有。在我的应用程序启动时,它会缓存数据库中的所有数据,并且永远不会添加新数据。我使用 Refresh() 方法,当我更改任何缓存数据时效果很好,但它不会刷新新添加的数据。

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

PS 应用程序在 app.config 中有不同的连接字符串(同一个 DB 的不同帐户)。

采纳答案by LukLed

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:

当你每次想使用它时刷新 store 时缓存有什么意义?你可以改成:

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.

它还会在每次调用时在数据库中查找,但操作要少得多。

回答by Rei Mavronicolas

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

从 EF 4.1 开始,您可以在实体上使用 AsNoTracking() 方法。

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.

请注意, AsNoTracking() 不会将实体添加到您的上下文中以进行跟踪,而只会从您的数据存储中返回它们。

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

有关更多信息,请参阅http://blogs.msdn.com/b/adonet/archive/2011/02/05/using-dbcontext-in-ef-feature-ctp5-part-11-load-and-asnotracking.aspx

回答by Matt

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

另一种可能性是使用 MergeOption 来决定如何管理上下文中的对象。例如,MergeOption.OverwriteChanges将使用来自数据源的值覆盖对象上下文。

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

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.data.objects.mergeoption.aspx