/// <summary> /// In this demo we are a query based on time (whose results are non-deterministic, because it uses DateTime.Now) /// Such queries are not cached, so we get a store query every time. /// /// Note that non-cacheable queries don't count as cache misses. /// </summary> private static void NonDeterministicQueryCachingDemo() { var cache = new InMemoryCache(); // log SQL from all connections to the console EFTracingProviderConfiguration.LogToConsole = true; for (int i = 0; i < 3; ++i) { Console.WriteLine(); Console.WriteLine("*** Pass #{0}...", i); Console.WriteLine(); using (var context = new ExtendedNorthwindEntities()) { // set up caching context.Cache = cache; context.CachingPolicy = CachingPolicy.CacheAll; Console.WriteLine("Loading orders..."); context.Orders.Where(c => c.ShippedDate < DateTime.Now).ToList(); } } Console.WriteLine(); Console.WriteLine("*** Cache statistics: Hits:{0} Misses:{1} Hit ratio:{2}% Adds:{3} Invalidations:{4}", cache.CacheHits, cache.CacheMisses, 100.0 * cache.CacheHits / (cache.CacheHits + cache.CacheMisses), cache.CacheItemAdds, cache.CacheItemInvalidations); }
private static void DdlMethodsDemo() { using (var context = new ExtendedNorthwindEntities()) { Console.WriteLine(context.CreateDatabaseScript()); } }
/// <summary> /// In this demo we are running simple queries and updates and we're logging the SQL commands /// using tracing events. /// </summary> private static void AdvancedTracingDemo() { // disable global logging to console EFTracingProviderConfiguration.LogToConsole = false; using (var context = new ExtendedNorthwindEntities()) { context.CommandExecuting += (sender, e) => { Console.WriteLine("Command is executing: {0}", e.ToTraceString()); }; context.CommandFinished += (sender, e) => { Console.WriteLine("Command has finished: {0}", e.ToTraceString()); }; var customer = context.Customers.First(c => c.CustomerID == "ALFKI"); customer.Orders.Load(); customer.ContactName = "Change" + Environment.TickCount; var newCustomer = new Customer() { CustomerID = "BELLA", CompanyName = "Bella Vision", ContactName = "Bella Bellissima", }; context.AddToCustomers(newCustomer); context.SaveChanges(); context.DeleteObject(newCustomer); context.SaveChanges(); } Console.WriteLine("LOG FILE CONTENTS:"); Console.WriteLine(File.ReadAllText("sqllogfile.txt")); }
/// <summary> /// In this demo we are running a set of queries and updates and 3 times and logging SQL commands to the console. /// Notice how performing an update on Customer table causes the cache entry to be invalidated so we get /// a query in each pass. Because we aren't modifying OrderDetails table, the collection of order details /// for the customer doesn't require a query in second and third pass. /// </summary> private static void CacheInvalidationDemo() { var cache = new InMemoryCache(); // log SQL from all connections to the console EFTracingProviderConfiguration.LogToConsole = true; for (int i = 0; i < 3; ++i) { Console.WriteLine(); Console.WriteLine("*** Pass #{0}...", i); Console.WriteLine(); using (var context = new ExtendedNorthwindEntities()) { // set up caching context.Cache = cache; context.CachingPolicy = CachingPolicy.CacheAll; Console.WriteLine("Loading customer..."); var cust = context.Customers.First(c => c.CustomerID == "ALFKI"); Console.WriteLine("Customer name: {0}", cust.ContactName); cust.ContactName = "Change" + Environment.TickCount; Console.WriteLine("Loading orders..."); cust.Orders.Load(); for (int o = 0; o < 10; ++o) { var order = new Order(); order.OrderDate = DateTime.Now; cust.Orders.Add(order); } Console.WriteLine("Order count: {0}", cust.Orders.Count); context.SaveChanges(); } } Console.WriteLine(); }
/// <summary> /// In this demo we are running a set of queries 3 times and logging SQL commands to the console. /// Note that queries are actually executed only in the first pass, while in second and third they are fulfilled /// completely from the cache. /// </summary> private static void SimpleCachingDemo() { ICache cache = new InMemoryCache(); CachingPolicy cachingPolicy = CachingPolicy.CacheAll; // log SQL from all connections to the console EFTracingProviderConfiguration.LogToConsole = true; for (int i = 0; i < 3; ++i) { Console.WriteLine(); Console.WriteLine("*** Pass #{0}...", i); Console.WriteLine(); using (var context = new ExtendedNorthwindEntities()) { // set up caching context.Cache = cache; context.CachingPolicy = cachingPolicy; Console.WriteLine("Loading customer..."); var cust = context.Customers.First(c => c.CustomerID == "ALFKI"); Console.WriteLine("Customer name: {0}", cust.ContactName); Console.WriteLine("Loading orders..."); cust.Orders.Load(); Console.WriteLine("Order count: {0}", cust.Orders.Count); } } Console.WriteLine(); //Console.WriteLine("*** Cache statistics: Hits:{0} Misses:{1} Hit ratio:{2}% Adds:{3} Invalidations:{4}", // cache.CacheHits, // cache.CacheMisses, // 100.0 * cache.CacheHits / (cache.CacheHits + cache.CacheMisses), // cache.CacheItemAdds, // cache.CacheItemInvalidations); }
/// <summary> /// In this demo we are running simple queries and updates and we're logging the SQL commands /// to the file. /// </summary> private static void SimpleTracingDemo() { // disable global logging to console EFTracingProviderConfiguration.LogToConsole = false; using (TextWriter logFile = File.CreateText("sqllogfile.txt")) { using (var context = new ExtendedNorthwindEntities()) { context.Log = logFile; // this will produce LIKE 'ALFKI%' T-SQL var customer = context.Customers.Single(c => c.CustomerID.StartsWith("ALFKI")); customer.Orders.Load(); customer.ContactName = "Change" + Environment.TickCount; var newCustomer = new Customer() { CustomerID = "BELLA", CompanyName = "Bella Vision", ContactName = "Bella Bellissima", }; context.AddToCustomers(newCustomer); context.SaveChanges(); context.DeleteObject(newCustomer); context.SaveChanges(); } } Console.WriteLine("LOG FILE CONTENTS:"); Console.WriteLine(File.ReadAllText("sqllogfile.txt")); }