Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
En este tema se describen los tipos de consultas LINQ to SQL que devuelven un objeto de la caché de identidades administrada por .DataContext
En LINQ to SQL, una de las formas en la que DataContext administra objetos es registrando las identidades de los objetos en una caché de identidades mientras se ejecutan las consultas. En algunos casos, LINQ to SQL intentará recuperar un objeto de la caché de identidades antes de ejecutar una consulta en la base de datos.
En general, para que una consulta LINQ to SQL devuelva un objeto de la caché de identidades, la consulta debe basarse en la clave principal de un objeto y debe devolver un único objeto. En concreto, la consulta debe estar en uno de los formularios generales que se muestran a continuación.
Nota:
Las consultas precompiladas no devolverán objetos de la caché de identidades. Para obtener más información sobre las consultas precompiladas, vea CompiledQuery y Cómo: Almacenar y reutilizar consultas.
Una consulta debe estar en uno de los siguientes formularios generales para recuperar un objeto de la caché de identidades:
Table<TEntity>
.Function1(predicate)Table<TEntity>
.Function1(predicate).Function2()
En estas formas generales, Function1, Function2y predicate se definen de la manera siguiente.
Function1 puede ser cualquiera de las siguientes:
Function2 puede ser cualquiera de las siguientes:
predicate debe ser una expresión en la que la propiedad de clave principal del objeto se establece en un valor constante. Si un objeto tiene una clave principal definida por más de una propiedad, cada propiedad de clave principal debe establecerse en un valor constante. A continuación se muestran ejemplos del formulario predicate que deben adoptar:
c => c.PK == constant_valuec => c.PK1 == constant_value1 && c=> c.PK2 == constant_value2
Ejemplo
El código siguiente proporciona ejemplos de los tipos de consultas LINQ to SQL que recuperan un objeto de la caché de identidades.
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)