Pobieranie obiektów z Identity Cache
W tym temacie opisano typy zapytań LINQ to SQL, które zwracają obiekt z pamięci podręcznej tożsamości zarządzanej przez DataContext.
W języku LINQ to SQL jednym ze sposobów, w jaki DataContext zarządza obiektami, jest rejestrowanie tożsamości obiektów w pamięci podręcznej tożsamości podczas wykonywania zapytań. W niektórych przypadkach LINQ to SQL podejmie próbę pobrania obiektu z cache'u tożsamości przed wykonaniem zapytania w bazie danych.
Ogólnie rzecz biorąc, aby zapytanie LINQ to SQL zwracało obiekt z pamięci podręcznej tożsamości, zapytanie musi być oparte na kluczu podstawowym obiektu i musi zwrócić pojedynczy obiekt. W szczególności zapytanie musi znajdować się w jednym z ogólnych formularzy przedstawionych poniżej.
Uwaga
Wstępnie skompilowane zapytania nie będą zwracać obiektów z pamięci podręcznej identyfikatorów. Aby uzyskać więcej informacji na temat wstępnie skompilowanych zapytań, zobacz CompiledQuery i Instrukcje: przechowywanie i ponowne używanie zapytań.
Aby pobrać obiekt z pamięci podręcznej tożsamości, zapytanie musi być w jednej z następujących form ogólnych.
Table<TEntity>
.Function1(
predicate
)
Table<TEntity>
.Function1(
predicate
).Function2()
W tych ogólnych formach Function1
, Function2
i predicate
są definiowane w następujący sposób.
Function1
może być jednym z następujących:
Function2
może być jednym z następujących:
predicate
musi być wyrażeniem, w którym właściwość klucza podstawowego obiektu jest ustawiona na wartość stałą. Jeśli obiekt ma klucz podstawowy zdefiniowany przez więcej niż jedną właściwość, każda właściwość klucza podstawowego musi być ustawiona na stałą wartość. Oto przykłady postaci, jaką musi przyjąć formularz predicate
:
c => c.PK == constant_value
c => c.PK1 == constant_value1 && c=> c.PK2 == constant_value2
Przykład
Poniższy kod zawiera przykłady typów zapytań LINQ do SQL, które pobierają obiekt z pamięci podręcznej tożsamości.
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 {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)
Zobacz też
- pojęcia dotyczące zapytań
- Tożsamość obiektu
- Informacje Podstawowe
- tożsamość obiektu