Aracılığıyla paylaş


Kimlik Önbelleğinden Nesne Alma

Bu konu, tarafından DataContextyönetilen kimlik önbelleğinden bir nesne döndüren LINQ to SQL sorgularının türlerini açıklar.

LINQ to SQL'de, nesneleri yönetmenin DataContext yollarından biri, sorgular yürütülürken nesne kimliklerini kimlik önbelleğinde günlüğe kaydetmektir. Bazı durumlarda LINQ to SQL veritabanında sorgu yürütmeden önce kimlik önbelleğinden bir nesne almaya çalışır.

Genel olarak, LINQ to SQL sorgusunun kimlik önbelleğinden bir nesne döndürmesi için sorgunun bir nesnenin birincil anahtarını temel alması ve tek bir nesne döndürmesi gerekir. Özellikle sorgunun aşağıda gösterilen genel formlardan birinde olması gerekir.

Not

Önceden derlenmiş sorgular, kimlik önbelleğinden nesne döndürmez. Önceden derlenmiş sorgular hakkında daha fazla bilgi için bkz CompiledQuery . ve Nasıl yapılır: Sorguları Depolama ve Yeniden Kullanma.

Kimlik önbelleğinden bir nesne almak için sorgu aşağıdaki genel formlardan birinde olmalıdır:

Bu genel formlarda , Function1Function2ve predicate aşağıdaki gibi tanımlanır.

Function1 aşağıdakilerden biri olabilir:

Function2 aşağıdakilerden biri olabilir:

predicate nesnenin birincil anahtar özelliğinin sabit bir değere ayarlandığı bir ifade olmalıdır. Bir nesnenin birden fazla özellik tarafından tanımlanan bir birincil anahtarı varsa, her birincil anahtar özelliği sabit bir değere ayarlanmalıdır. Aşağıda formun predicate alması gereken örnekler verilmiştir:

  • c => c.PK == constant_value

  • c => c.PK1 == constant_value1 && c=> c.PK2 == constant_value2

Örnek

Aşağıdaki kod, kimlik önbelleğinden bir nesne alan LINQ to SQL sorgusu türlerinin örneklerini sağlar.

NorthwindDataContext context = new NorthwindDataContext();

// This query does not retrieve an object from
// the query cache because it is the first query.
// There are no objects in the cache.
var a = context.Customers.First();
Console.WriteLine("First query gets customer {0}. ", a.CustomerID);

// This query returns an object from the query cache.
var b = context.Customers.Where(c => c.CustomerID == a.CustomerID);
foreach (var customer in b )
{
    Console.WriteLine(customer.CustomerID);
}

// This query returns an object from the identity cache.
// Note that calling FirstOrDefault(), Single(), or SingleOrDefault()
// instead of First() will also return an object from the cache.
var x = context.Customers.
    Where(c => c.CustomerID == a.CustomerID).
    First();
Console.WriteLine(x.CustomerID);

// This query returns an object from the identity cache.
// Note that calling FirstOrDefault(), Single(), or SingleOrDefault()
// instead of First() (each with the same predicate) will also
// return an object from the cache.
var y = context.Customers.First(c => c.CustomerID == a.CustomerID);
Console.WriteLine(y.CustomerID);
Dim context As New NorthwindDataContext()

' This query does not retrieve an object from
' the query cache because it is the first query.
' There are no objects in the cache. 
Dim a = context.Customers.First()
Console.WriteLine("First query gets customer {0}. ", a.CustomerID)

' This query returns an object from the query cache.
Dim b = context.Customers.Where(Function(c) c.CustomerID = a.CustomerID)
For Each customer In b
    Console.WriteLine(customer.CustomerID)
Next

' This query returns an object from the identity cache.
' Note that calling FirstOrDefault(), Single(), or SingleOrDefault()
' instead of First() will also return an object from the cache.
Dim x = context.Customers. _
    Where(Function(c) c.CustomerID = a.CustomerID). _
    First()
Console.WriteLine(x.CustomerID)

' This query returns an object from the identity cache.
' Note that calling FirstOrDefault(), Single(), or SingleOrDefault()
' instead of First() (each with the same predicate) will also
' return an object from the cache.
Dim y = context.Customers.First(Function(c) c.CustomerID = a.CustomerID)
Console.WriteLine(y.CustomerID)

Ayrıca bkz.