Example #1
0
 private static void InsertDestination()
 {
     Destination destination = new Destination
     {
         Country = "Indonesia",
         Description = "EcoTourism at its best in esquisite Bali",
         Name = "Bali"
     };
     using(BreakAwayContext context = new BreakAwayContext())
     {
         context.Destinations.Add(destination);
         context.SaveChanges();
     }
 }
        //----------------------------------------------
        //Chapter 2 - Adding Changing And Deleting Entities
        //----------------------------------------------


        //Adding New Entities
        //--------------------------

        private static void AddMachuPicchu()
        {
            using (var context = new BreakAwayContext())
            {
                var machuPicchu = new Destination
                {
                    Name = "Machu Picchu",
                    Country = "Peru"
                };

                context.Destinations.Add(machuPicchu);
                context.SaveChanges();
            }
        }
        //Multiple Changes at Once
        private static void MakeMultipleChanges()
        {
            using (var context = new BreakAwayContext())
            {
                var niagaraFalls = new Destination
                {
                    Name = "Niagara Falls",
                    Country = "USA"
                };

                context.Destinations.Add(niagaraFalls);

                var wineGlassBay = (from d in context.Destinations
                    where d.Name == "Wine Glass Bay"
                    select d).Single();

                wineGlassBay.Description = "Picturesque bay with beaches.";

                context.SaveChanges();

            }
        }
        private List<Destination> ReadExcelData(OleDbConnection conn, string sheetName)
        {
            Console.WriteLine("Reading data...");
            var excelDbCommand = new OleDbCommand(@"SELECT * FROM [" + sheetName + "]", conn);
            using (var oleDbDataAdapter = new OleDbDataAdapter(excelDbCommand))
            {
                DataSet ds = new DataSet();
                oleDbDataAdapter.Fill(ds);
                var destinations = new List<Destination>();
                using (var reader = ds.CreateDataReader())
                {
                    while (reader.Read())
                    {
                        var destination = new Destination();
                        destination.Country = reader["Country"].ToString();
                        destination.Distance = double.Parse(reader["Distance"].ToString());
                        destination.LuxuryFactor = int.Parse(reader["LuxuryFactor"].ToString());
                        destinations.Add(destination);
                    }
                }

                return destinations;
            }
        }
Example #5
0
        private static void DeleteDestinationInMemoryAndDbCascade()
        {
            int destinationId;
            using(BreakAwayContext context = new BreakAwayContext())
            {
                Destination destination = new Destination
                {
                    Name = "Sample Destination",
                    Lodgings = new List<Lodging>
                    {
                        new Lodging { Name = "Lodging One" },
                        new Lodging { Name = "Lodging Two" }
                    }
                };
                context.Destinations.Add(destination);
                context.SaveChanges();
                destinationId = destination.DestinationId;
            }

            using(BreakAwayContext context = new BreakAwayContext())
            {
                Destination destination = context.Destinations.
                    Single(n => n.DestinationId == destinationId);
                context.Destinations.Remove(destination);
                context.SaveChanges();
            }

            using (BreakAwayContext context = new BreakAwayContext())
            {
                List<Lodging> lodgings = context.Lodgings.Where(n => n.DestinationId == destinationId).ToList();
                Console.WriteLine("Lodgings: {0}", lodgings.Count);
            }
        }