private static void Cleanup() { using (var context = new EFRecipesEntities()) { context.Database.ExecuteSqlCommand("delete from chapter13.resume"); } }
private static void RunExample() { using (var context = new EFRecipesEntities()) { var r1 = new Resume { Title = "C# Developer", Name = "Sally Jones" }; r1.ResumeDetail = new ResumeDetail { Body = "...very long resume goes here..." }; context.Resumes.Add(r1); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var resume = context.Resumes.Single(); Console.WriteLine("Title: {0}, Name: {1}", resume.Title, resume.Name); // note, the ResumeDetail is not loaded until we reference it Console.WriteLine("Body: {0}", resume.ResumeDetail.Body); } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter6.employee"); } }
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { using (var context = new EFRecipesEntities()) { // delete all test data context.ExecuteStoreCommand("delete from chapter4.reservation"); context.ExecuteStoreCommand("delete from chapter4.hotel"); // insert new test data var h1 = new Hotel { Name = "Riverside Inn" }; var h2 = new Hotel { Name = "Greenville Inn" }; context.Reservations.AddObject(new Reservation { Name = "Robin Rosen", ReservationDate = DateTime.Parse("4/20/2010"), Rate = 99.95M, Hotel = h1 }); context.Reservations.AddObject(new Reservation { Name = "James Marlowe", ReservationDate = DateTime.Parse("5/18/2010"), Rate = 105.00M, Hotel = h2 }); context.SaveChanges(); } } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var man1 = new Manager { Name = "Jill Stevens" }; var proj = new Project { Name = "City Riverfront Park", Manager = man1 }; var con1 = new Contractor { Name = "Robert Alvert", Project = proj }; var con2 = new Contractor { Name = "Alan Jones", Project = proj }; var con3 = new Contractor { Name = "Nancy Roberts", Project = proj }; context.Projects.AddObject(proj); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var project = context.Projects.Include("Manager").First(); if (project.ManagerReference.IsLoaded) { Console.WriteLine("Manager entity is loaded."); } else { Console.WriteLine("Manager entity is NOT loaded."); } if (project.Contractors.IsLoaded) { Console.WriteLine("Contractors are loaded."); } else { Console.WriteLine("Contractors are NOT loaded."); } Console.WriteLine("Calling project.Contractors.Load()..."); project.Contractors.Load(); if (project.Contractors.IsLoaded) { Console.WriteLine("Contractors are now loaded."); } else { Console.WriteLine("Contractors failed to load."); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
public void Update(Client client) { using (var context = new EFRecipesEntities()) { context.Entry(client).State = EntityState.Modified; context.SaveChanges(); } }
public Client GetClient() { using (var context = new EFRecipesEntities()) { context.Configuration.LazyLoadingEnabled = false; return(context.Clients.Single()); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter3.book"); context.ExecuteStoreCommand("delete from chapter3.category"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter2.retail"); context.ExecuteStoreCommand("delete from chapter2.eCommerce"); context.ExecuteStoreCommand("delete from chapter2.business"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter5.contractor"); context.ExecuteStoreCommand("delete from chapter5.project"); context.ExecuteStoreCommand("delete from chapter5.manager"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter15.friend"); context.ExecuteStoreCommand("delete from chapter15.residence"); context.ExecuteStoreCommand("delete from chapter15.relative"); } }
public void Update(Client client) { using (var context = new EFRecipesEntities()) { context.Clients.Attach(client); context.ObjectStateManager.ChangeObjectState(client, EntityState.Modified); context.SaveChanges(); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter11.[order]"); // we're re-using customer, so we need to delete from invoice as well! context.ExecuteStoreCommand("delete from chapter11.invoice"); context.ExecuteStoreCommand("delete from chapter11.customer"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var cat1 = new Category { Name = "Programming" }; var cat2 = new Category { Name = "Databases" }; var cat3 = new Category { Name = "Operating Systems" }; context.Books.AddObject(new Book { Title = "F# In Practice", Category = cat1 }); context.Books.AddObject(new Book { Title = "The Joy of SQL", Category = cat2 }); context.Books.AddObject(new Book { Title = "Windows 7: The Untold Story", Category = cat3 }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Books (using LINQ)"); List <string> cats = new List <string> { "Programming", "Databases" }; var books = from b in context.Books where cats.Contains(b.Category.Name) select b; foreach (var book in books) { Console.WriteLine("'{0}' is in category: {1}", book.Title, book.Category.Name); } } using (var context = new EFRecipesEntities()) { Console.WriteLine("Books (using ESQL)"); var esql = @"select value b from Books as b where b.Category.Name in {'Programming','Databases'}"; var books = context.CreateQuery <Book>(esql); foreach (var book in books) { Console.WriteLine("'{0}' is in category: {1}", book.Title, book.Category.Name); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
public void InsertTestRecord() { using (var context = new EFRecipesEntities()) { // delete previous test data context.ExecuteStoreCommand("delete from chapter9.client"); // insert new test data context.ExecuteStoreCommand(@"insert into chapter9.client(Name, Email) values ('Jerry Jones','*****@*****.**')"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var employee1 = new Employee { EmployeeNumber = 629, Name = "Robin Rosen", Salary = 106000M }; var employee2 = new Employee { EmployeeNumber = 147, Name = "Bill Moore", Salary = 62500M }; var task1 = new Task { Description = "Report 3rd Qtr Accounting" }; var task2 = new Task { Description = "Forecast 4th Qtr Sales" }; var task3 = new Task { Description = "Prepare Sales Tax Report" }; // use AddObject() on the Employees entity set context.Employees.AddObject(employee1); // add two new tasks to the employee1's tasks employee1.Tasks.Add(task1); employee1.Tasks.Add(task2); // add a task to the employee and use // AddObject() to add the task to the object context employee2.Tasks.Add(task3); context.Tasks.AddObject(task3); // persist all of these to the database context.SaveChanges(); } using (var context = new EFRecipesEntities()) { foreach (var employee in context.Employees) { Console.WriteLine("Employee: {0}'s Tasks", employee.Name); foreach (var task in employee.Tasks) { Console.WriteLine("\t{0}", task.Description); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var c1 = new Customer { Name = "Jill Masters", City = "Raytown" }; var c2 = new Customer { Name = "Bob Meyers", City = "Austin" }; var c3 = new Customer { Name = "Robin Rosen", City = "Dallas" }; var o1 = new Order { OrderAmount = 12.99M, Customer = c1 }; var o2 = new Order { OrderAmount = 99.39M, Customer = c2 }; var o3 = new Order { OrderAmount = 101.29M, Customer = c3 }; context.Orders.AddObject(o1); context.Orders.AddObject(o2); context.Orders.AddObject(o3); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Customers with above average total purchases"); var esql = @"select o.Customer.Name, count(o.OrderId) as TotalOrders, Sum(o.OrderAmount) as TotalPurchases from EFRecipesEntities.Orders as o where o.OrderAmount > anyelement(select value Avg(o.OrderAmount) from EFRecipesEntities.Orders as o) group by o.Customer.Name"; var summary = context.CreateQuery <DbDataRecord>(esql); foreach (var item in summary) { Console.WriteLine("\t{0}, Total Orders: {1}, Total: {2:C}", item["Name"], item["TotalOrders"], item["TotalPurchases"]); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var business = new Business { Name = "Corner Dry Cleaning", LicenseNumber = "100x1" }; context.Businesses.AddObject(business); var retail = new Retail { Name = "Shop and Save", LicenseNumber = "200C", Address = "101 Main", City = "Anytown", State = "TX", ZIPCode = "76106" }; context.Businesses.AddObject(retail); var web = new eCommerce { Name = "BuyNow.com", LicenseNumber = "300AB", URL = "www.buynow.com" }; context.Businesses.AddObject(web); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("\n--- All Businesses ---"); foreach (var b in context.Businesses) { Console.WriteLine("{0} (#{1})", b.Name, b.LicenseNumber); } Console.WriteLine("\n--- Retail Businesses ---"); foreach (var r in context.Businesses.OfType <Retail>()) { Console.WriteLine("{0} (#{1})", r.Name, r.LicenseNumber); Console.WriteLine("{0}", r.Address); Console.WriteLine("{0}, {1} {2}", r.City, r.State, r.ZIPCode); } Console.WriteLine("\n--- eCommerce Businesses ---"); foreach (var e in context.Businesses.OfType <eCommerce>()) { Console.WriteLine("{0} (#{1})", e.Name, e.LicenseNumber); Console.WriteLine("Online address is: {0}", e.URL); } } Console.WriteLine("Press <enter> to continue.."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var hourly = new HourlyEmployee { Name = "Will Smith", Hours = 39, Rate = 7.75M }; var salaried = new SalariedEmployee { Name = "JoAnn Woodland", Salary = 65400M }; var commissioned = new CommissionedEmployee { Name = "Joel Clark", Salary = 32500M, Commission = 20M }; context.Employees.AddObject(hourly); context.Employees.AddObject(salaried); context.Employees.AddObject(commissioned); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("All Employees"); Console.WriteLine("============="); foreach (var emp in context.Employees) { if (emp is HourlyEmployee) { Console.WriteLine("{0} Hours = {1}, Rate = {2}/hour", emp.Name, ((HourlyEmployee)emp).Hours.Value.ToString(), ((HourlyEmployee)emp).Rate.Value.ToString("C")); } else if (emp is CommissionedEmployee) { Console.WriteLine("{0} Salary = {1}, Commission = {2}%", emp.Name, ((CommissionedEmployee)emp).Salary.Value.ToString("C"), ((CommissionedEmployee)emp).Commission.ToString()); } else if (emp is SalariedEmployee) { Console.WriteLine("{0} Salary = {1}", emp.Name, ((SalariedEmployee)emp).Salary.Value.ToString("C")); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var res1 = new FriendResidence { Address = "123 Main", City = "Anytown", State = "CA", ZIP = "90210" }; var res2 = new RelativeResidence { Address = "1200 East Street", City = "Big Town", State = "KS", ZIP = "66026" }; var f = new Friend { Name = "Joan Roland", FriendResidence = res1 }; var r = new Relative { Name = "Billy Miner", RelativeResidence = res2 }; context.Friends.AddObject(f); context.Relatives.AddObject(r); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { context.ContextOptions.LazyLoadingEnabled = true; foreach (var r in context.Residences) { if (r is FriendResidence) { Console.WriteLine("My friend {0} lives at: ", ((FriendResidence)r).Friend.Name); } else if (r is RelativeResidence) { Console.WriteLine("My relative {0} lives at: ", ((RelativeResidence)r).Relative.Name); } Console.WriteLine("\t{0}", r.Address); Console.WriteLine("\t{0}, {1} {2}", r.City, r.State, r.ZIP); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.Athletes.AddObject(new Athlete { Name = "Nancy Steward", Height = 167, Weight = 53 }); context.Athletes.AddObject(new Athlete { Name = "Rob Achers", Height = 170, Weight = 77 }); context.Athletes.AddObject(new Athlete { Name = "Chuck Sanders", Height = 171, Weight = 82 }); context.Athletes.AddObject(new Athlete { Name = "Nancy Rodgers", Height = 166, Weight = 59 }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { // do a delete and an update var all = context.Athletes; context.DeleteObject(all.Where(o => o.Name == "Nancy Steward").First()); all.Where(o => o.Name == "Rob Achers").First().Weight = 80; context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("All Athletes"); Console.WriteLine("============"); foreach (var athlete in context.Athletes) { Console.WriteLine("{0} weighs {1} Kg and is {2} cm in height", athlete.Name, athlete.Weight, athlete.Height); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var emp1 = new Employee { Name = "Roger Smith", Salary = 108000M }; var emp2 = new Employee { Name = "Jane Hall", Salary = 81500M }; context.Employees.AddObject(emp1); context.Employees.AddObject(emp2); context.SaveChanges(); emp1.Salary = emp1.Salary * 1.5M; try { context.SaveChanges(); } catch (Exception) { Console.WriteLine("Oops, tried to increase a salary too much!"); } } using (var context = new EFRecipesEntities()) { Console.WriteLine(); Console.WriteLine("Employees"); foreach (var emp in context.Employees) { Console.WriteLine("{0} makes {1}/year", emp.Name, emp.Salary.ToString("C")); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var r1 = new Resume { Title = "C# Developer", Name = "Sally Jones", Body = "...very long resume goes here..." }; context.Resumes.AddObject(r1); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { // using eSQL var query = "select value Recipe8.Resume(r.ResumeId, r.Title, r.Name,'') from Resumes as r"; var result1 = context.CreateQuery <Resume>(query).Single(); Console.WriteLine("Resume body: {0}", result1.Body); context.Resumes.MergeOption = MergeOption.OverwriteChanges; var result2 = context.Resumes.Single(); Console.WriteLine("Resume body: {0}", result2.Body); } using (var context = new EFRecipesEntities()) { // using ExecuteStoreQuery() var result1 = context.ExecuteStoreQuery <Resume>("select ResumeId, Title, Name,'' Body from chapter13.Resume", "Resumes", MergeOption.AppendOnly).Single(); Console.WriteLine("Resume body: {0}", result1.Body); var result2 = context.ExecuteStoreQuery <Resume>("select * from chapter13.Resume", "Resumes", MergeOption.OverwriteChanges).Single(); Console.WriteLine("Resume body: {0}", result2.Body); } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
public ReservationRepository() { this.context = new EFRecipesEntities(); }
public HotelRepository() { this.context = new EFRecipesEntities(); }