예제 #1
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter8.employee");
     }
 }
예제 #2
0
 static void InsertData()
 {
     using (var context = new EFRecipesEntities())
     {
         var c1 = new College {
             Name = "Mid-Missouri State"
         };
         var c2 = new College {
             Name = "Florida Western University"
         };
         var p1 = new Lawyer {
             Name = "Bill Jordan", College = c1
         };
         p1.CourtDates.Add(new CourtDate {
             Appointment = DateTime.Parse("3/02/10 10:00 am"), Name = "John Dillinger"
         });
         var p2 = new Accountant {
             Name = "Sue Redmon", College = c2
         };
         p2.Clients.Add(new Client {
             Name = "Robin Rosen"
         });
         context.People.AddObject(p1);
         context.People.AddObject(p2);
         context.SaveChanges();
         Console.WriteLine("Data inserted...");
     }
 }
예제 #3
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter14.forumpost");
     }
 }
예제 #4
0
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var context = new EFRecipesEntities())
            {
                context.ExecuteStoreCommand("delete from chapter4.[order]");
                context.ExecuteStoreCommand("delete from chapter4.webcustomer");

                var cust1 = new WebCustomer {
                    Name = "Joan Steward"
                };
                var cust2 = new WebCustomer {
                    Name = "Allen Colbert"
                };
                var cust3 = new WebCustomer {
                    Name = "Phil Marlowe"
                };
                var order1 = new Order {
                    Amount = 29.95M, OrderDate = DateTime.Parse("3/18/2010")
                };
                var order2 = new Order {
                    Amount = 84.99M, OrderDate = DateTime.Parse("3/20/2010")
                };
                var order3 = new Order {
                    Amount = 99.95M, OrderDate = DateTime.Parse("4/10/2010")
                };
                order1.WebCustomer = cust1;
                order2.WebCustomer = cust2;
                order3.WebCustomer = cust3;
                context.Orders.AddObject(order1);
                context.Orders.AddObject(order2);
                context.Orders.AddObject(order3);
                context.SaveChanges();
            }
        }
예제 #5
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var teacher = new Teacher {
                    Name = "Susan Smith", School = "Custer Baker Middle School"
                };
                var firefighter = new Firefighter {
                    Name = "Joel Clark", FireStation = "Midtown"
                };
                var retired = new Retired {
                    Name = "Joan Collins", FullTimeHobby = "Scapbooking"
                };
                context.People.AddObject(teacher);
                context.People.AddObject(firefighter);
                context.People.AddObject(retired);
                context.SaveChanges();
                firefighter.Hero = teacher;
                teacher.Hero     = retired;
                retired.Hero     = firefighter;
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = true;
                foreach (var person in context.People)
                {
                    if (person.Hero != null)
                    {
                        Console.WriteLine("\n{0}, Hero is: {1}", person.Name, person.Hero.Name);
                    }
                    else
                    {
                        Console.WriteLine("{0}", person.Name);
                    }
                    if (person is Firefighter)
                    {
                        Console.WriteLine("Firefighter at station {0}", ((Firefighter)person).FireStation);
                    }
                    else if (person is Teacher)
                    {
                        Console.WriteLine("Teacher at {0}", ((Teacher)person).School);
                    }
                    else if (person is Retired)
                    {
                        Console.WriteLine("Retired, hobby is {0}", ((Retired)person).FullTimeHobby);
                    }
                    Console.WriteLine("Fans:");
                    foreach (var fan in person.Fans)
                    {
                        Console.WriteLine("\t{0}", fan.Name);
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #6
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter12.CartItem");
         context.ExecuteStoreCommand("delete from chapter12.Cart");
     }
 }
예제 #7
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter5.event");
         context.ExecuteStoreCommand("delete from chapter5.club");
     }
 }
예제 #8
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter2.orderitem");
         context.ExecuteStoreCommand("delete from chapter2.[order]");
         context.ExecuteStoreCommand("delete from chapter2.item");
     }
 }
예제 #9
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter3.teacher");
         context.ExecuteStoreCommand("delete from chapter3.lawyer");
         context.ExecuteStoreCommand("delete from chapter3.person");
     }
 }
예제 #10
0
 static void GetTimes()
 {
     using (var context = new EFRecipesEntities())
     {
         var stopwatch = new Stopwatch();
         stopwatch.Start();
         var lawyer = context.People.Include("College").OfType <Lawyer>().Include("CourtDates").First();
         stopwatch.Stop();
         Console.WriteLine("Execution Time: {0}", stopwatch.ElapsedMilliseconds);
     }
 }
예제 #11
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter13.courtdate");
         context.ExecuteStoreCommand("delete from chapter13.client");
         context.ExecuteStoreCommand("delete from chapter13.accountant");
         context.ExecuteStoreCommand("delete from chapter13.lawyer");
         context.ExecuteStoreCommand("delete from chapter13.person");
         context.ExecuteStoreCommand("delete from chapter13.college");
     }
 }
예제 #12
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                context.People.AddObject(new Teacher {
                    Name = "Janet Dietz", IsProfessor = true
                });
                context.People.AddObject(new Teacher {
                    Name = "Robert Kline", IsProfessor = false
                });
                context.People.AddObject(new Lawyer {
                    Name = "Jenny Dunlap", Cases = 3
                });
                context.People.AddObject(new Lawyer {
                    Name = "Karen Eads", Cases = 7
                });
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                var esql     = "select value p from OfType(People,Recipe4.Teacher) as p";
                var teachers = context.CreateQuery <Teacher>(esql);
                Console.WriteLine("Teachers...Using Object Services");
                foreach (var teacher in teachers)
                {
                    Console.WriteLine("{0} is{1} a professor", teacher.Name, teacher.IsProfessor ? "" : " not");
                }
            }

            Console.WriteLine();

            using (var conn = new EntityConnection("name=EFRecipesEntities"))
            {
                conn.Open();
                var esql = "select value p from OfType(EFRecipesEntities.People,EFRecipesModel.Teacher) as p";
                var cmd  = conn.CreateCommand();
                cmd.CommandText = esql;
                Console.WriteLine("Teachers...Using EntityClient");
                using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    while (reader.Read())
                    {
                        Console.WriteLine("{0} is{1} a professor", reader.GetString(1), reader.GetBoolean(2) ? "" : " not");
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #13
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                context.Employees.AddObject(new Employee {
                    Name = new Name {
                        FirstName = "Annie", LastName = "Oakley"
                    }, Email = "*****@*****.**"
                });
                context.Employees.AddObject(new Employee {
                    Name = new Name {
                        FirstName = "Bill", LastName = "Jordan"
                    }, Email = "*****@*****.**"
                });
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                foreach (var employee in context.Employees.OrderBy(e => e.Name.LastName))
                {
                    Console.WriteLine("{0}, {1} email: {2}", employee.Name.LastName, employee.Name.FirstName, employee.Email);
                }
            }

            int id = 0;

            using (var context = new EFRecipesEntities())
            {
                var emp = context.Employees.Where(e => e.Name.FirstName.StartsWith("Bill")).FirstOrDefault();
                id = emp.EmployeeId;
            }

            using (var context = new EFRecipesEntities())
            {
                var empDelete = new Employee {
                    EmployeeId = id, Name = new Name {
                        FirstName = string.Empty, LastName = string.Empty
                    }
                };
                context.Employees.Attach(empDelete);
                context.Employees.DeleteObject(empDelete);
                context.SaveChanges();
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #14
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var order = new Order {
                    OrderId = 1, OrderDate = new DateTime(2010, 1, 18)
                };
                var item = new Item {
                    SKU = 1729, Description = "Backpack", Price = 29.97M
                };
                var oi = new OrderItem {
                    Order = order, Item = item, Count = 1
                };
                item = new Item {
                    SKU = 2929, Description = "Water Filter", Price = 13.97M
                };
                oi = new OrderItem {
                    Order = order, Item = item, Count = 3
                };
                item = new Item {
                    SKU = 1847, Description = "Camp Stove", Price = 43.99M
                };
                oi = new OrderItem {
                    Order = order, Item = item, Count = 1
                };
                context.Orders.AddObject(order);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = true;
                foreach (var order in context.Orders)
                {
                    Console.WriteLine("Order # {0}, ordered on {1}", order.OrderId.ToString(), order.OrderDate.ToShortDateString());
                    Console.WriteLine("SKU\tDescription\tQty\tPrice");
                    Console.WriteLine("---\t-----------\t---\t-----");
                    foreach (var oi in order.OrderItems)
                    {
                        Console.WriteLine("{0}\t{1}\t{2}\t{3}", oi.Item.SKU, oi.Item.Description, oi.Count.ToString(), oi.Item.Price.ToString("C"));
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #15
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var emp1 = new Employee {
                    Name = "Lisa Jefferies", Address = new EmployeeAddress {
                        Address = "100 E. Main", City = "Fort Worth", State = "TX", ZIP = "76106"
                    }
                };
                var emp2 = new Employee {
                    Name = "Robert Jones", Address = new EmployeeAddress {
                        Address = "3920 South Beach", City = "Fort Worth", State = "TX", ZIP = "76102"
                    }
                };
                var emp3 = new Employee {
                    Name = "Steven Chue", Address = new EmployeeAddress {
                        Address = "129 Barker", City = "Euless", State = "TX", ZIP = "76092"
                    }
                };
                var emp4 = new Employee {
                    Name = "Karen Stevens", Address = new EmployeeAddress {
                        Address = "108 W. Parker", City = "Fort Worth", State = "TX", ZIP = "76102"
                    }
                };
                context.AddToEmployees(emp1);
                context.AddToEmployees(emp2);
                context.AddToEmployees(emp3);
                context.AddToEmployees(emp4);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Employee addresses in Fort Worth, TX");
                foreach (var address in context.GetEmployeeAddresses("Fort Worth"))
                {
                    Console.WriteLine("{0}, {1}, {2}, {3}", address.Address, address.City, address.State, address.ZIP);
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #16
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var item1 = new CartItem {
                    SKU = "AMM-223", Quantity = 3, Price = 19.95M
                };
                var item2 = new CartItem {
                    SKU = "CAMP-12", Quantity = 1, Price = 59.95M
                };
                var item3 = new CartItem {
                    SKU = "29292", Quantity = 2, Price = 4.95M
                };
                var cart = new Cart {
                    CartTotal = 0
                };
                cart.CartItems.Add(item1);
                cart.CartItems.Add(item2);
                cart.CartItems.Add(item3);
                context.Carts.AddObject(cart);
                item1.Quantity = 1;
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                foreach (var cart in context.Carts)
                {
                    Console.WriteLine("Cart Total = {0}", cart.CartTotal.ToString("C"));
                    foreach (var item in cart.CartItems)
                    {
                        Console.WriteLine("\tSKU = {0}, Qty = {1}, Unit Price = {2}", item.SKU, item.Quantity.ToString(), item.Price.ToString("C"));
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #17
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var club = new Club {
                    Name = "Star City Chess Club", City = "New York"
                };
                context.Clubs.AddObject(club);
                new Event {
                    EventName = "Mid Cities Tournament", EventDate = DateTime.Parse("1/09/2010"), Club = club
                };
                new Event {
                    EventName = "State Finals Tournament", EventDate = DateTime.Parse("2/12/2010"), Club = club
                };
                new Event {
                    EventName = "Winter Classic", EventDate = DateTime.Parse("12/18/2009"), Club = club
                };
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                var events = from ev in context.Events
                             where ev.Club.City == "New York"
                             group ev by ev.Club into g
                             select g.FirstOrDefault(e1 => e1.EventDate == g.Min(evt => evt.EventDate));

                var e = ((ObjectQuery <Event>)events).Include("Club").First();
                Console.WriteLine("The next New York club event is:");
                Console.WriteLine("\tEvent: {0}", e.EventName);
                Console.WriteLine("\tDate: {0}", e.EventDate.ToShortDateString());
                Console.WriteLine("\tClub: {0}", e.Club.Name);
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #18
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var p = new Principal { Name = "Jill Robins", HireDate = DateTime.Parse("8/12/2002"), Salary = 72500M };
                var i1 = new Instructor { Name = "Roland Jones", HireDate = DateTime.Parse("8/14/2005"), Salary = 61000M};
                var i2 = new Instructor { Name = "Steven Curtis", HireDate = DateTime.Parse("8/23/1992"), Salary = 68200M };
                context.People.AddObject(new Student { Name = "Karen Roberts" });
                context.People.AddObject(new Student {Name = "Bobby McGivens"});
                context.People.AddObject(new Student {Name = "Janis Hettler"});
                context.People.AddObject(p);
                context.People.AddObject(i1);
                context.People.AddObject(i2);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Staff");
                Console.WriteLine("=====");
                foreach (var staff in context.People.OfType<Staff>())
                {
                    Console.WriteLine("\t{0}, Hire date: {1}, Salary: {2} {3})", staff.Name, staff.HireDate.Value.ToShortDateString(),
                        staff.Salary.Value.ToString("C"), staff is Principal ? "Principal" : "Instructor");
                }
                Console.WriteLine("\nStudents");
                Console.WriteLine("==========");
                foreach (var student in context.People.OfType<Student>())
                {
                    Console.WriteLine("\t{0}", student.Name);
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #19
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var john = new Supervisor {
                    Name = "John Smith"
                };
                var steve = new Supervisor {
                    Name = "Steve Johnson"
                };
                var jill = new ProjectManager {
                    Name = "Jill Masterson", Manager = john
                };
                var karen = new ProjectManager {
                    Name = "Karen Carns", Manager = steve
                };
                var bob = new TeamLead {
                    Name = "Bob Richardson", Manager = karen
                };
                var tom = new TeamLead {
                    Name = "Tom Landers", Manager = jill
                };
                var nancy = new TeamMember {
                    Name = "Nancy Jones", Manager = tom
                };
                var stacy = new TeamMember {
                    Name = "Stacy Rutgers", Manager = bob
                };
                context.Associates.AddObject(john);
                context.Associates.AddObject(steve);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Using eSql...");
                var emps = context.Associates.OfType <TeamMember>()
                           .Where(@"EFRecipesModel.GetProjectManager(it).Name = 
                           @projectManager || 
                           EFRecipesModel.GetSupervisor(it).Name == @supervisor",
                                  new ObjectParameter("projectManager", "Jill Masterson"),
                                  new ObjectParameter("supervisor", "Steve Johnson"));
                Console.WriteLine("Team members that report up to either");
                Console.WriteLine("Project Manager Jill Masterson ");
                Console.WriteLine("or Supervisor Steve Johnson");
                foreach (var emp in emps)
                {
                    Console.WriteLine("\tAssociate: {0}", emp.Name);
                }
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine();
                Console.WriteLine("Using LINQ...");
                var emps = from e in context.Associates.OfType <TeamMember>()
                           where MyFunctions.GetProjectManager(e).Name ==
                           "Jill Masterson" ||
                           MyFunctions.GetSupervisor(e).Name == "Steve Johnson"
                           select e;
                Console.WriteLine("Team members that report up to either");
                Console.WriteLine("Project Manager Jill Masterson ");
                Console.WriteLine("or Supervisor Steve Johnson");
                foreach (var emp in emps)
                {
                    Console.WriteLine("\tAssociate: {0}", emp.Name);
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #20
0
        static void RunExample()
        {
            int postId = 0;

            using (var context = new EFRecipesEntities())
            {
                // post is created
                var post = new ForumPost {
                    ForumUser = "******", IsActive = false, Post = "The moderator is a great guy."
                };
                context.ForumPosts.AddObject(post);
                context.SaveChanges();
                postId = post.PostingId;
            }

            using (var context = new EFRecipesEntities())
            {
                // moderator gets post to review
                var post = context.ForumPosts.First(p => p.PostingId == postId);
                Console.WriteLine("Post by {0}: {1}", post.ForumUser, post.Post);

                // poster changes post out-of-band
                context.ExecuteStoreCommand(@"update chapter14.forumpost 
                         set post='The moderator''s mom dresses him funny.' 
                         where postingId = @p0", new object[] { postId.ToString() });
                Console.WriteLine("Fast Eddie changes the post");

                // moderator doesn't trust Fast Eddie
                if (string.Compare(post.ForumUser, "FastEddie27") == 0)
                {
                    post.IsActive = false;
                }
                else
                {
                    post.IsActive = true;
                }

                try
                {
                    // refresh any changes to the TimeStamp
                    context.ForumPosts.MergeOption = MergeOption.PreserveChanges;
                    post = context.ForumPosts.First(p => p.PostingId == postId);
                    context.SaveChanges();
                    Console.WriteLine("No concurrency exception.");
                }
                catch (OptimisticConcurrencyException)
                {
                    try
                    {
                        context.Refresh(RefreshMode.ClientWins, post);
                        context.SaveChanges();
                    }
                    catch (OptimisticConcurrencyException)
                    {
                        // we tried twice...do something else
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }