/// <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> /// 创建新的 Customer 对象。 /// </summary> /// <param name="customerID">CustomerID 属性的初始值。</param> /// <param name="companyName">CompanyName 属性的初始值。</param> /// <param name="address">Address 属性的初始值。</param> public static Customer CreateCustomer(global::System.String customerID, global::System.String companyName, CustomerAddress address) { Customer customer = new Customer(); customer.CustomerID = customerID; customer.CompanyName = companyName; customer.Address = StructuralObject.VerifyComplexObjectIsNotNull(address, "Address"); return customer; }
/// <summary> /// 用于向 Customers EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet<T> 属性的 .Add 方法。 /// </summary> public void AddToCustomers(Customer customer) { base.AddObject("Customers", customer); }
/// <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")); }