エンティティコンテキストは、データベースからデータを1回だけ取得してから、メモリにキャッシュします。
データベースからデータを取得するには、最初SaveChanges()
にContext1を呼び出す必要があります。次に、を呼び出しRefresh(RefreshMode.StoreWins, Context2.EntityToRefresh)
て、context2のデータベース値を取得します。
共有/静的コンテキストを使用してクエリを実行することもできます。これにより、すべてのクエリで同じデータを確実に取得できます。
このように実装できます
public class SharedObjectContext
{
private readonly WestwindEntities context;
#region Singleton Pattern
// Static members are lazily initialized.
// .NET guarantees thread safety for static initialization.
private static readonly SharedObjectContext instance = new SharedObjectContext();
// Make the constructor private to hide it.
// This class adheres to the singleton pattern.
private SharedObjectContext()
{
// Create the ObjectContext.
context = new WestwindEntities();
}
// Return the single instance of the ClientSessionManager type.
public static SharedObjectContext Instance
{
get
{
return instance;
}
}
#endregion
public WestwindEntities Context
{
get
{
return context;
}
}
}