public List <Project> RetrieveAll() { using (var context = new EFRecipesEntities()) { return(context.Projects.ToList()); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter12.donation"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { using (var scope1 = new TransactionScope()) { // save, but don't commit var outerEmp = new Employee { Name = "Karen Stanfield" }; Console.WriteLine("Outer employee: {0}", outerEmp.Name); context.Employees.AddObject(outerEmp); context.SaveChanges(); // second transaction for read uncommitted using (var innerContext = new EFRecipesEntities()) { using (var scope2 = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted })) { var innerEmp = innerContext.Employees.First(e => e.Name == "Karen Stanfield"); Console.WriteLine("Inner employee: {0}", innerEmp.Name); scope1.Complete(); scope2.Complete(); } } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.Donations.AddObject(new Donation { DonorName = "Robert Byrd", Amount = 350M }); context.Donations.AddObject(new Donation { DonorName = "Nancy McVoid", Amount = 250M }); context.Donations.AddObject(new Donation { DonorName = "Kim Kerns", Amount = 750M }); Console.WriteLine("About to SaveChanges()"); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var list = context.Donations.Where(o => o.Amount > 300M); Console.WriteLine("Donations over $300"); foreach (var donor in list) { Console.WriteLine("{0} gave {1}", donor.DonorName, donor.Amount.ToString("C")); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample2() { using (var context = new EFRecipesEntities()) { var product1 = new Product { Name = "Pole", Price = 12.97M }; var product2 = new Product { Name = "Tent", Price = 199.95M }; var product3 = new Product { Name = "Ground Cover", Price = 29.95M }; product2.RelatedProducts.Add(product3); product1.RelatedProducts.Add(product2); context.Products.AddObject(product1); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var product1 = context.Products.First(p => p.Name == "Pole"); Dictionary <int, Product> t = new Dictionary <int, Product>(); GetRelated(product1, t); Console.WriteLine("Products related to {0}", product1.Name); foreach (var key in t.Keys) { Console.WriteLine("\t{0}", t[key].Name); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter10.atmwithdrawal"); context.ExecuteStoreCommand("delete from chapter10.atmmachine"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter6.relatedproduct"); context.ExecuteStoreCommand("delete from chapter6.product"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter8.violation"); context.ExecuteStoreCommand("delete from chapter8.ticket"); context.ExecuteStoreCommand("delete from chapter8.vehicle"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter2.linktable"); context.ExecuteStoreCommand("delete from chapter2.artist"); context.ExecuteStoreCommand("delete from chapter2.album"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter13.appointment"); context.ExecuteStoreCommand("delete from chapter13.doctor"); context.ExecuteStoreCommand("delete from chapter13.company"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter15.ProjectEmployee"); context.ExecuteStoreCommand("delete from chapter15.project"); context.ExecuteStoreCommand("delete from chapter15.employee"); } }
public void UpdateProject(Project project, Project originalProject) { // First approach using (var context = new EFRecipesEntities()) { context.Projects.Attach(project); context.Projects.ApplyOriginalValues(originalProject); context.SaveChanges(); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter5.plumber"); context.ExecuteStoreCommand("delete from chapter5.foreman"); context.ExecuteStoreCommand("delete from chapter5.jobsite"); context.ExecuteStoreCommand("delete from chapter5.location"); context.ExecuteStoreCommand("delete from chapter5.tradesman"); context.ExecuteStoreCommand("delete from chapter5.phone"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var vh1 = new Vehicle { LicenseNo = "BR-549" }; var t1 = new Ticket { IssueDate = DateTime.Parse("4/18/10") }; var v1 = new Violation { Description = "20 MPH over the speed limit", Amount = 125M }; var v2 = new Violation { Description = "Broken tail light", Amount = 50M }; t1.Violations.Add(v1); t1.Violations.Add(v2); t1.Vehicle = vh1; context.Tickets.AddObject(t1); var vh2 = new Vehicle { LicenseNo = "XJY-902" }; var t2 = new Ticket { IssueDate = DateTime.Parse("4/20/10") }; var v3 = new Violation { Description = "Parking in a no parking zone", Amount = 35M }; t2.Violations.Add(v3); t2.Vehicle = vh2; context.Tickets.AddObject(t2); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { context.ContextOptions.LazyLoadingEnabled = true; foreach (var ticket in context.Tickets) { Console.WriteLine(" Ticket: {0}, Total Cost: {1}", ticket.TicketId.ToString(), ticket.Violations.Sum(v => v.Amount).ToString("C")); foreach (var violation in ticket.Violations) { Console.WriteLine("\t{0}", violation.Description); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { using (var context = new EFRecipesEntities()) { // delete any previous test data context.ExecuteStoreCommand("delete from chapter9.project"); // insert some test data context.Projects.AddObject(new Project { Name = "Trim City Park Trees", AmountAllocated = 8200M, PercentComplete = 75 }); context.SaveChanges(); } } }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.Employees.AddObject(new Employee { FirstName = "Jill", LastName = "Robins", Birthdate = DateTime.Parse("3/2/1976") }); context.Employees.AddObject(new Employee { FirstName = "Michael", LastName = "Kirk", Birthdate = DateTime.Parse("4/12/1985") }); context.Employees.AddObject(new Employee { FirstName = "Karen", LastName = "Stanford", Birthdate = DateTime.Parse("6/18/1963") }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Query using eSql"); var esql = @"Select EFRecipesModel.FullName(e) as Name, EFRecipesModel.Age(e) as Age from EFRecipesEntities.Employees as e"; var emps = context.CreateQuery <DbDataRecord>(esql); foreach (var emp in emps) { Console.WriteLine("Employee: {0}, Age: {1}", emp["Name"], emp["Age"]); } } using (var context = new EFRecipesEntities()) { Console.WriteLine("\nQuery using LINQ"); var emps = from e in context.Employees select new { Name = MyFunctions.FullName(e), Age = MyFunctions.Age(e) }; foreach (var emp in emps) { Console.WriteLine("Employee: {0}, Age: {1}", emp.Name, emp.Age.ToString()); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var foreman1 = new Foreman { Name = "Carl Ramsey" }; var foreman2 = new Foreman { Name = "Nancy Ortega" }; var phone = new Phone { Number = "817 867-5309" }; var jobsite = new JobSite { JobSiteName = "City Arena", Address = "123 Main", City = "Anytown", State = "TX", ZIPCode = "76082", Phone = phone }; jobsite.Foremen.Add(foreman1); jobsite.Foremen.Add(foreman2); var plumber = new Plumber { Name = "Jill Nichols", Email = "*****@*****.**", JobSite = jobsite }; context.Tradesmen.AddObject(plumber); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var plumber = context.Tradesmen.OfType <Plumber>().Include("JobSite.Phone").Include("JobSite.Foremen").First(); Console.WriteLine("Plumber's Name: {0} ({1})", plumber.Name, plumber.Email); Console.WriteLine("Job Site: {0}", plumber.JobSite.JobSiteName); Console.WriteLine("Job Site Phone: {0}", plumber.JobSite.Phone.Number); Console.WriteLine("Job Site Foremen:"); foreach (var boss in plumber.JobSite.Foremen) { Console.WriteLine("\t{0}", boss.Name); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var proj = new Project { Name = "Highway 101 Access Route" }; proj.Members.Add(new Employee { Name = "Jim Stone" }); proj.Members.Add(new Employee { Name = "Roland Jones" }); proj.Members.Add(new Employee { Name = "Jennifer Collins" }); proj.ProjectManager = new Employee { Name = "Sue Raven" }; context.Projects.AddObject(proj); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { context.ContextOptions.LazyLoadingEnabled = true; foreach (var p in context.Projects) { Console.WriteLine("Project: {0}, Manager: {1}", p.Name, p.ProjectManager.Name); Console.WriteLine("Members:"); foreach (var m in p.Members) { Console.WriteLine("\t{0}", m.Name); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { DateTime today = DateTime.Parse("2/2/2010"); DateTime yesterday = DateTime.Parse("2/1/2010"); using (var context = new EFRecipesEntities()) { var atm = new ATMMachine { ATMId = 17, Location = "12th and Main" }; atm.ATMWithdrawals.Add(new ATMWithdrawal { Amount = 20.00M, Date = today }); atm.ATMWithdrawals.Add(new ATMWithdrawal { Amount = 100.00M, Date = today }); atm.ATMWithdrawals.Add(new ATMWithdrawal { Amount = 75.00M, Date = yesterday }); atm.ATMWithdrawals.Add(new ATMWithdrawal { Amount = 50.00M, Date = today }); context.AddToATMMachines(atm); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var forToday = context.GetWithdrawls(17, today).FirstOrDefault(); var forYesterday = context.GetWithdrawls(17, yesterday).FirstOrDefault(); var atm = context.ATMMachines.Where(o => o.ATMId == 17).FirstOrDefault(); Console.WriteLine("ATM Withdrawals for ATM at {0} at {1}", atm.ATMId.ToString(), atm.Location); Console.WriteLine("\t{0} Total Withdrawn = {1}", yesterday.ToShortDateString(), forYesterday.Value.ToString("C")); Console.WriteLine("\t{0} Total Withdrawn = {1}", today.ToShortDateString(), forToday.Value.ToString("C")); } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var company = new Company { Name = "Paola Heart Center" }; var doc1 = new Doctor { Name = "Jill Mathers", Company = company }; var doc2 = new Doctor { Name = "Robert Stevens", Company = company }; var app1 = new Appointment { AppointmentDate = DateTime.Parse("3/18/2010"), Patient = "Karen Rodgers", Doctor = doc1 }; var app2 = new Appointment { AppointmentDate = DateTime.Parse("3/20/2010"), Patient = "Steven Cook", Doctor = doc2 }; context.Companies.AddObject(company); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Appointments for Doctors..."); context.Doctors.MergeOption = MergeOption.NoTracking; var doctors = context.Doctors.Include("Company"); foreach (var doctor in doctors) { Console.WriteLine("Doctor: {0} [{1}]", doctor.Name, doctor.Company.Name); Console.WriteLine("Appointments: {0}", doctor.Appointments.Count().ToString()); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var product1 = new Product { Name = "Pole", Price = 12.97M }; var product2 = new Product { Name = "Tent", Price = 199.95M }; var product3 = new Product { Name = "Ground Cover", Price = 29.95M }; product2.RelatedProducts.Add(product3); product1.RelatedProducts.Add(product2); context.Products.AddObject(product1); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var product2 = context.Products.Include("RelatedProducts").Include("OtherRelatedProducts").First(p => p.Name == "Tent"); Console.WriteLine("Product: {0} ... {1}", product2.Name, product2.Price.ToString("C")); Console.WriteLine("Related Products"); foreach (var prod in product2.RelatedProducts) { Console.WriteLine("\t{0} ... {1}", prod.Name, prod.Price.ToString("C")); } foreach (var prod in product2.OtherRelatedProducts) { Console.WriteLine("\t{0} ... {1}", prod.Name, prod.Price.ToString("C")); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { // add an artist with two albums var artist = new Artist { FirstName = "Alan", LastName = "Jackson" }; var album1 = new Album { AlbumName = "Drive" }; var album2 = new Album { AlbumName = "Live at Texas Stadium" }; artist.Albums.Add(album1); artist.Albums.Add(album2); context.Artists.AddObject(artist); // add an album for two artists var artist1 = new Artist { FirstName = "Tobby", LastName = "Keith" }; var artist2 = new Artist { FirstName = "Merle", LastName = "Haggard" }; var album = new Album { AlbumName = "Honkytonk University" }; artist1.Albums.Add(album); artist2.Albums.Add(album); context.Albums.AddObject(album); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { context.ContextOptions.LazyLoadingEnabled = true; Console.WriteLine("Artists and their albums..."); var artists = from a in context.Artists select a; foreach (var artist in artists) { Console.WriteLine("{0} {1}", artist.FirstName, artist.LastName); foreach (var album in artist.Albums) { Console.WriteLine("\t{0}", album.AlbumName); } } Console.WriteLine("\nAlbums and their artists..."); var albums = from a in context.Albums select a; foreach (var album in albums) { Console.WriteLine("{0}", album.AlbumName); foreach (var artist in album.Artists) { Console.WriteLine("\t{0} {1}", artist.FirstName, artist.LastName); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var cus1 = new Customer { Name = "Robert Stevens", Email = "*****@*****.**" }; var cus2 = new Customer { Name = "Julia Kerns", Email = "*****@*****.**" }; var cus3 = new Customer { Name = "Nancy Whitrock", Email = "*****@*****.**" }; context.Customers.AddObject(cus1); context.Customers.AddObject(cus2); context.Customers.AddObject(cus3); context.SaveChanges(); } // using object services using (var context = new EFRecipesEntities()) { Console.WriteLine("Customers..."); string esql = "select value c from Customers as c"; var customers = context.CreateQuery <Customer>(esql); foreach (var customer in customers) { Console.WriteLine("{0}'s email is: {1}", customer.Name, customer.Email); } } Console.WriteLine(); // using EntityClient using (var conn = new EntityConnection("name=EFRecipesEntities")) { Console.WriteLine("Customers..."); var cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = "select value c from EFRecipesEntities.Customers as c"; using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { while (reader.Read()) { Console.WriteLine("{0}'s email is: {1}", reader.GetString(1), reader.GetString(2)); } } } Console.WriteLine(); // using object services without the VALUE keyword using (var context = new EFRecipesEntities()) { Console.WriteLine("Customers..."); string esql = "select c.Name, c.Email from Customers as c"; var records = context.CreateQuery <DbDataRecord>(esql); foreach (var record in records) { var name = record[0] as string; var email = record[1] as string; Console.WriteLine("{0}'s email is: {1}", name, email); } } Console.WriteLine(); // using EntityClient without the VALUE keyword using (var conn = new EntityConnection("name=EFRecipesEntities")) { Console.WriteLine("Customers..."); var cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = "select c.Name, C.Email from EFRecipesEntities.Customers as c"; using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { while (reader.Read()) { Console.WriteLine("{0}'s email is: {1}", reader.GetString(0), reader.GetString(1)); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }