static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter12.PurchaseOrder"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter2.photograph"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.PurchaseOrders.AddObject(new PurchaseOrder { Amount = 109.98M }); context.PurchaseOrders.AddObject(new PurchaseOrder { Amount = 20.99M }); context.PurchaseOrders.AddObject(new PurchaseOrder { Amount = 208.89M }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Purchase Orders"); foreach (var po in context.PurchaseOrders) { Console.WriteLine("Purchase Order: {0}", po.PurchaseOrderId.ToString("")); Console.WriteLine("\tPaid: {0}", po.Paid ? "Yes" : "No"); Console.WriteLine("\tAmount: {0}", po.Amount.ToString("C")); Console.WriteLine("\tCreated On: {0}", po.CreateDate.ToShortTimeString()); Console.WriteLine("\tModified at: {0}", po.ModifiedDate.ToShortTimeString()); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var order = new Order { CustomerName = "Jenny Craig", OrderDate = DateTime.Parse("3/12/2010") }; var item1 = new OrderItem { Order = order, Shipped = 3, SKU = 2827, UnitPrice = 12.95M }; var item2 = new OrderItem { Order = order, Shipped = 1, SKU = 1918, UnitPrice = 19.95M }; var item3 = new OrderItem { Order = order, Shipped = 3, SKU = 392, UnitPrice = 8.95M }; context.Orders.AddObject(order); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { // assume we have an instance of Order var order = context.Orders.First(); // get the total order amount var amt = order.OrderItems.CreateSourceQuery().Sum(o => (o.Shipped * o.UnitPrice)); Console.WriteLine("Order Number: {0}", order.OrderId.ToString()); Console.WriteLine("Order Date: {0}", order.OrderDate.ToShortDateString()); Console.WriteLine("Order Total: {0}", amt.ToString("C")); } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var principal = new Principal { Name = "Robbie Smith", Bonus = 3500M, Salary = 48000M }; var instructor = new Instructor { Name = "Joan Carlson", Salary = 39000M }; context.Staffs.AddObject(principal); context.Staffs.AddObject(instructor); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Principals"); Console.WriteLine("=========="); foreach (var p in context.Staffs.OfType <Principal>()) { Console.WriteLine("\t{0}, Salary: {1}, Bonus: {2}", p.Name, p.Salary.ToString("C"), p.Bonus.ToString("C")); } Console.WriteLine("Instructors"); Console.WriteLine("==========="); foreach (var i in context.Staffs.OfType <Instructor>()) { Console.WriteLine("\t{0}, Salary: {1}", i.Name, i.Salary.ToString("C")); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { byte[] thumbBits = new byte[100]; byte[] fullBits = new byte[2000]; using (var context = new EFRecipesEntities()) { var photo = new Photograph { PhotoId = 1, Title = "My Dog", ThumbnailBits = thumbBits }; var fullImage = new PhotographFullImage { PhotoId = 1, HighResolutionBits = fullBits }; photo.PhotographFullImage = fullImage; context.Photographs.AddObject(photo); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { foreach (var photo in context.Photographs) { Console.WriteLine("Photo: {0}, ThumbnailSize {1} bytes", photo.Title, photo.ThumbnailBits.Length.ToString()); // explicitly load the "expensive" entity, PhotographFullImage photo.PhotographFullImageReference.Load(); Console.WriteLine("Full Image Size: {0} bytes", photo.PhotographFullImage.HighResolutionBits.Length.ToString()); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
protected void Page_Load(object sender, EventArgs e) { using (var context = new EFRecipesEntities()) { // delete any previous test data context.ExecuteStoreCommand("delete from chapter4.item"); context.ExecuteStoreCommand("delete from chapter4.itemcategory"); // populate with some test data var cat1 = new ItemCategory { Name = "Tents" }; var cat2 = new ItemCategory { Name = "Cooking Equipment" }; context.Items.AddObject(new Item { Name = "Backpacking Tent", ItemCategory = cat1 }); context.Items.AddObject(new Item { Name = "Camp Stove", ItemCategory = cat2 }); context.Items.AddObject(new Item { Name = "Dutch Oven", ItemCategory = cat2 }); context.Items.AddObject(new Item { Name = "Alpine Tent", ItemCategory = cat1 }); context.Items.AddObject(new Item { Name = "Fire Starter", ItemCategory = cat2 }); context.SaveChanges(); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter13.paycheck"); context.ExecuteStoreCommand("delete from chapter13.associate"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter5.orderitem"); context.ExecuteStoreCommand("delete from chapter5.[order]"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter3.bid"); context.ExecuteStoreCommand("delete from chapter3.job"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter8.speakertalk"); context.ExecuteStoreCommand("delete from chapter8.speaker"); context.ExecuteStoreCommand("delete from chapter8.talk"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter6.Instructor"); context.ExecuteStoreCommand("delete from chapter6.PRincipal"); context.ExecuteStoreCommand("delete from chapter6.staff"); } }
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { using (var context = new EFRecipesEntities()) { // delete previous test data context.ExecuteStoreCommand("delete from chapter9.phone"); context.ExecuteStoreCommand("delete from chapter9.customer"); } } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var job1 = new Job { JobDetails = "Re-surface Parking Log" }; var job2 = new Job { JobDetails = "Build Driveway" }; job1.Bids.Add(new Bid { Amount = 948M, Bidder = "ABC Paving" }); job1.Bids.Add(new Bid { Amount = 1028M, Bidder = "TopCoat Paving" }); job2.Bids.Add(new Bid { Amount = 502M, Bidder = "Ace Concrete" }); context.Jobs.AddObject(job1); context.Jobs.AddObject(job2); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var cs = @"Data Source=.;Initial Catalog=EFRecipes;Integrated Security=True"; var conn = new SqlConnection(cs); var cmd = conn.CreateCommand(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = "Chapter3.GetBidDetails"; conn.Open(); var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); var jobs = context.Translate <Job>(reader, "Jobs", MergeOption.AppendOnly).ToList(); reader.NextResult(); context.Translate <Bid>(reader, "Bids", MergeOption.AppendOnly).ToList(); foreach (var job in jobs) { Console.WriteLine("\nJob: {0}", job.JobDetails); foreach (var bid in job.Bids) { Console.WriteLine("\tBid: {0} from {1}", bid.Amount.ToString("C"), bid.Bidder); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var speaker1 = new Speaker { Name = "Karen Stanfield" }; var talk1 = new Talk { Title = "Simulated Annealing in C#" }; speaker1.Talks = new List <Talk> { talk1 }; // associations not yet complete Console.WriteLine("talk1.Speaker is null: {0}", talk1.Speakers == null); context.Speakers.AddObject(speaker1); // now it's fixed up Console.WriteLine("talk1.Speaker is null: {0}", talk1.Speakers == null); Console.WriteLine("Number of added entries tracked: {0}", context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added).Count()); context.SaveChanges(); // change the talk's title talk1.Title = "AI with C# in 3 Easy Steps"; Console.WriteLine("talk1's state is: {0}", context.ObjectStateManager.GetObjectStateEntry(talk1).State); context.DetectChanges(); Console.WriteLine("talk1's state is: {0}", context.ObjectStateManager.GetObjectStateEntry(talk1).State); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { foreach (var speaker in context.Speakers.Include("Talks")) { Console.WriteLine("Speaker: {0}", speaker.Name); foreach (var talk in speaker.Talks) { Console.WriteLine("\tTalk Title: {0}", talk.Title); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.People.AddObject(new Instructor { Name = "Karen Stanford", Salary = 62500M }); context.People.AddObject(new Instructor { Name = "Robert Morris", Salary = 61800M }); context.People.AddObject(new Student { Name = "Jill Mathers", Degree = "Computer Science" }); context.People.AddObject(new Student { Name = "Steven Kennedy", Degree = "Math" }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Instructors and Students"); var allPeople = context.GetAllPeople(); foreach (var person in allPeople) { if (person is Instructor) { Console.WriteLine("Instructor {0} makes {1}/year", person.Name, ((Instructor)person).Salary.ToString("C")); } else if (person is Student) { Console.WriteLine("Student {0}'s major is {1}", person.Name, ((Student)person).Degree); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
public CustomerRepository() { this.context = new EFRecipesEntities(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var a1 = new Associate { Name = "Robert Stevens", City = "Raytown" }; a1.Paychecks.Add(new Paycheck { PayDate = DateTime.Parse("3/1/10"), Gross = 1802.83M }); a1.Paychecks.Add(new Paycheck { PayDate = DateTime.Parse("3/15/10"), Gross = 1924.91M }); var a2 = new Associate { Name = "Karen Thorp", City = "Gladstone" }; a2.Paychecks.Add(new Paycheck { PayDate = DateTime.Parse("3/1/10"), Gross = 2102.34M }); a2.Paychecks.Add(new Paycheck { PayDate = DateTime.Parse("3/15/10"), Gross = 1992.18M }); context.Associates.AddObject(a1); context.Associates.AddObject(a2); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Stopwatch watch = new Stopwatch(); long totalTicks = 0; // warm things up context.Associates.Where(a => a.Name.StartsWith("Karen")).ToList(); // query gets compiled each time for (int i = 0; i < 10; i++) { watch.Restart(); context.Associates.Where(a => a.Name.StartsWith("Karen")).ToList(); watch.Stop(); totalTicks += watch.ElapsedTicks; Console.WriteLine("Not Compiled: {0}", watch.ElapsedTicks.ToString()); } Console.WriteLine("Average ticks without compiling: {0}", (totalTicks / 10).ToString()); Console.WriteLine(""); // compile the query just once and re-use var query = CompiledQuery.Compile <EFRecipesEntities, IQueryable <Associate> >(ctx => from a in ctx.Associates where a.Name.StartsWith("Karen") select a); totalTicks = 0; for (int i = 0; i < 10; i++) { watch.Restart(); query(context).ToList(); watch.Stop(); totalTicks += watch.ElapsedTicks; Console.WriteLine("Compiled: {0}", watch.ElapsedTicks.ToString()); } Console.WriteLine("Average ticks with compiling: {0}", (totalTicks / 10).ToString()); } using (var context = new EFRecipesEntities()) { // an example of returning an anonymous type var list = CompiledQuery.Compile((EFRecipesEntities ctx) => from a in ctx.Associates select new { Name = a.Name, TotalPay = a.Paychecks.Sum(p => p.Gross) }); var everyone = list(context).ToList(); } using (var context = new EFRecipesEntities()) { // an example of composing compiled and non-compiled queries var query = CompiledQuery.Compile <EFRecipesEntities, string, IQueryable <Associate> >((ctx, city) => ctx.Associates.Where(c => c.City == city)); var highlyPaid = from a in query(context, "Raytown") where a.Paychecks.Average(p => p.Gross) > 1500M select a; var associates = highlyPaid.ToList(); } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }