DarioSantarelli.Blog("UgiDotNet");

<sharing mode=”On” users=”*” />
posts - 176, comments - 105, trackbacks - 3

My Links

News


This is my personal blog. These postings are provided "AS IS" with no warranties, and confer no rights.




Tag Cloud

Archives

Post Categories

My English Blog

[Entity Framework] Disabilitare il change tracking dell’ObjectStateManager

(da MSDN) ObjectStateManager tracks query results, and provides logic to merge multiple overlapping query results. It also performs in-memory change tracking when a user inserts, deletes, or modifies objects, and provides the change set for updates. This change set is used by the change processor to persist modifications.


La classe ObjectStateManager in generale gestisce lo stato dei nostri EntityObject. In quei casi in cui i nostri dati devono essere semplicemente presentati in modalità read only può essere utile disabilitare il change tracking dell’ ObjectContext, ottenendo un incremento di performance talvolta significativo. 
In particolare, per disabilitare il change tracking occorre utilizzare l’opzione MergeOption.NoTracking dell’enumeration MergeOption, che permette di lavorare con Entità aventi EntityStateDetached’. Fondamentalmente esistono due modi per disabilitare il change tracking:

  1. Utilizzando l’oggetto ObjectQuery. Esiste infatti un overload del costruttore che permette di specificare l’opzione MergeOption desiderata.
  2. A livello di EntitySet

Vediamo entrambi gli esempi:

Caso 1

using (NorthwindObjectContext context = new NorthwindObjectContext())
{
 
ObjectQuery<Customer> queryCustomers = new ObjectQuery<Customer>("SELECT VALUE c FROM NorthwindObjectContext.Customers AS c", context, MergeOption.NoTracking);
 
foreach (Customer customer in queryCustomers)   
 
{
   
Console.WriteLine("{0} - {1}", customer.ContactName, customer.EntityState.ToString());    
 
}
}


Caso 2

using (NorthwindObjectContext context = new NorthwindObjectContext())
{   
 
context.Customers.MergeOption = MergeOption.NoTracking;    
 
foreach (Customer customer in context.Customers)   
 
{
   
Console.WriteLine("{0} - {1}", customer.ContactName, customer.EntityState.ToString());    
 
}
}


Technorati Tag:

Print | posted on martedì 3 marzo 2009 23:31 |

Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET