static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter3.Customer"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.Invoices.AddObject(new Invoice { Amount = 19.95M, Description = "Oil Change", Date = DateTime.Parse("4/11/10") }); context.Invoices.AddObject(new Invoice { Amount = 129.95M, Description = "Wheel Alignment", Date = DateTime.Parse("4/01/10") }); context.Invoices.AddObject(new DeletedInvoice { Amount = 39.95M, Description = "Engine Diagnosis", Date = DateTime.Parse("4/01/10") }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { foreach (var invoice in context.Invoices) { var isDeleted = invoice as DeletedInvoice; Console.WriteLine("{0} Invoice", isDeleted == null ? "Active" : "Deleted"); Console.WriteLine("Description: {0}", invoice.Description); Console.WriteLine("Amount: {0}", invoice.Amount.ToString("C")); Console.WriteLine("Date: {0}", invoice.Date.ToShortDateString()); Console.WriteLine(); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
private static void Cleanup() { using (var context = new EFRecipesEntities()) { context.Database.ExecuteSqlCommand("delete from chapter5.invoice"); context.Database.ExecuteSqlCommand("delete from chapter5.client"); } }
private static void RunExample() { using (var context = new EFRecipesEntities()) { var client1 = new Client { Name = "Karen Standfield", ClientId = 1 }; var invoice1 = new Invoice { InvoiceDate = DateTime.Parse("4/1/10"), Amount = 29.95M }; var invoice2 = new Invoice { InvoiceDate = DateTime.Parse("4/2/10"), Amount = 49.95M }; var invoice3 = new Invoice { InvoiceDate = DateTime.Parse("4/3/10"), Amount = 102.95M }; var invoice4 = new Invoice { InvoiceDate = DateTime.Parse("4/4/10"), Amount = 45.99M }; // Add invoice to client's collection client1.Invoices.Add(invoice1); // Assign the foreign key directly invoice2.ClientId = 1; // Attach() and existing row using a "fake" entity context.Database.ExecuteSqlCommand("insert into chapter5.client values (2, 'Phil Marlowe')"); var client2 = new Client { ClientId = 2 }; context.Clients.Attach(client2); invoice3.Client = client2; // Using the ClientReference invoice4.Client = client1; // Save the changes context.Clients.Add(client1); context.Invoices.Add(invoice2); context.Invoices.Add(invoice3); context.Invoices.Add(invoice4); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { foreach (var client in context.Clients) { Console.WriteLine("Client: {0}", client.Name); foreach (var invoice in client.Invoices) { Console.WriteLine("\t{0} for {1}", invoice.InvoiceDate.ToShortDateString(), invoice.Amount.ToString("C")); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.Customers.AddObject(new Customer { Name = "Roberts, Jill", Email = "*****@*****.**" }); context.Customers.AddObject(new Customer { Name = "Robertson, Alice", Email = "*****@*****.**" }); context.Customers.AddObject(new Customer { Name = "Rogers, Steven", Email = "*****@*****.**" }); context.Customers.AddObject(new Customer { Name = "Roe, Allen", Email = "*****@*****.**" }); context.Customers.AddObject(new Customer { Name = "Jones, Chris", Email = "*****@*****.**" }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { string match = "Ro"; int pageIndex = 0; int pageSize = 3; var customers = context.Customers.Where(c => c.Name.StartsWith(match)) .OrderBy(c => c.Name) .Skip(pageIndex * pageSize) .Take(pageSize); Console.WriteLine("Customers Ro*"); foreach (var customer in customers) { Console.WriteLine("{0} [email: {1}]", customer.Name, customer.Email); } } using (var context = new EFRecipesEntities()) { string match = "Ro%"; int pageIndex = 0; int pageSize = 3; var customers = context.Customers.Where("it.Name like @Name", new ObjectParameter("Name", match)) .Skip("it.Name", "@Skip", new ObjectParameter("Skip", pageIndex)) .Top("@Limit", new ObjectParameter("Limit", pageSize)); Console.WriteLine("\nCustomers Ro*"); foreach (var customer in customers) { Console.WriteLine("{0} [email: {1}]", customer.Name, customer.Email); } } using (var context = new EFRecipesEntities()) { string match = "Ro%"; int pageIndex = 0; int pageSize = 3; var esql = @"select value c from Customers as c where c.Name like @Name order by c.Name skip @Skip limit @Limit"; Console.WriteLine("\nCustomers Ro*"); var customers = context.CreateQuery <Customer>(esql, new[] { new ObjectParameter("Name", match), new ObjectParameter("Skip", pageIndex * pageSize), new ObjectParameter("Limit", pageSize) }); foreach (var customer in customers) { Console.WriteLine("{0} [email: {1}]", customer.Name, customer.Email); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }