private static void PrintAllInventory() {
     using (AutoLotEntities context = new AutoLotEntities()) {
         foreach (Car c in context.Cars) {
             Console.WriteLine(c.ToString());
         }
     }
 }
        private static void FunWithLINQQueries()
        {
            using (AutoLotEntities context = new AutoLotEntities())
            {
                //  获取新数据的投影
                var colorsMakes = from item in context.Cars
                                  select new
                {
                    item.Color,
                    item.Make
                };
                foreach (var item in colorsMakes)
                {
                    Console.WriteLine(item);
                }

                //  只获取CarID<1000的记录
                var idsLessThan1000 = from item in context.Cars
                                      where item.CarID < 1000
                                      select item;
                foreach (var item in idsLessThan1000)
                {
                    Console.WriteLine(item);
                }
            }
        }
 private static void PrintAllInventory()
 {
     using (AutoLotEntities context = new AutoLotEntities())
     {
         foreach (Car c in context.Cars)
         {
             Console.WriteLine(c);
         }
     }
 }
 private static void PrintAllInventory()
 {
     //  选择AutoLot中Inventory表的所有记录,并使用Car实体类的自定义ToString()方法打印其数据
     using (AutoLotEntities context = new AutoLotEntities())
     {
         foreach (Car c in context.Cars)
         {
             Console.WriteLine(c);
         }
     }
 }
Beispiel #5
0
 private static void PrintAllInventory()
 {
     // Select all items from the Inventory table of AutoLot,
     // and print out the data using our custom ToString()
     // of the Car entity class.
     using (AutoLotEntities context = new AutoLotEntities())
     {
         foreach (Car c in context.Cars)
         {
             Console.WriteLine(c);
         }
     }
 }
 public static void UpdateRecord() {
     using (AutoLotEntities context = new AutoLotEntities()) {
         //   EntityKey key = new EntityKey("AutoLotEntities.Cars", "CarID", 2224);
         Car c = context.Cars.Find(2224);
         Console.WriteLine(string.Format("Found : {0}", c));
         if (c != null) {
             c.Color = "Blue";                    
             context.SaveChanges();
         }
         Console.WriteLine("changed 2224 to blue");
         // Car carToDelete = context.
     }
 }
 private static void RemoveRecord()
 {
     using (AutoLotEntities context = new AutoLotEntities())
     {
         //查找实体,如果存在则将其删除
         Car carToDelete = (Car)context.Cars.Where(g => g.CarID == 2222).Select(g => g).FirstOrDefault();
         if (carToDelete != null)
         {
             context.Cars.Remove(carToDelete);
             context.SaveChanges();
         }
     }
 }
        private static void UpdateRecord()
        {
            using (AutoLotEntities context = new AutoLotEntities())
            {
                Car carToUpdate = (from car in context.Cars where car.CarID == 2222 select car).FirstOrDefault();

                if (carToUpdate != null)
                {
                    carToUpdate.CarNickName = "LiPengfei";
                    context.SaveChanges();
                }
            }
        }
 public static void RemoveRecord() {
     using (AutoLotEntities context = new AutoLotEntities()) {
      //   EntityKey key = new EntityKey("AutoLotEntities.Cars", "CarID", 2224);
       //  Car c = context.Cars.Find(2224);
         Car c = (from car in context.Cars where car.CarID == 2224 select car).FirstOrDefault();
        Console.WriteLine(string.Format("Found : {0}" ,c));
        if (c != null) {
            context.Cars.Remove(c);
            context.SaveChanges();
        }
        Console.WriteLine("removed 2224");
        // Car carToDelete = context.
     }
 }
Beispiel #10
0
        private static void RemoveRecordWithLINQ()
        {
            // Find a car to delete by primary key.
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // See if we have it.
                var carToDelete = (from c in context.Cars where c.CarID == 2222 select c).FirstOrDefault();

                if (carToDelete != null)
                {
                    context.DeleteObject(carToDelete);
                    context.SaveChanges();
                }
            }
        }
Beispiel #11
0
        private static void FunWithEntitySQL()
        {
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // Build a string containing Entity SQL syntax.
                string query = "SELECT VALUE car FROM AutoLotEntities.Cars AS car WHERE car.Color='black'";

                // Now build a ObjectQuery<T> based on the string.
                var blackCars = context.CreateQuery<Car>(query);

                foreach (var item in blackCars)
                {
                    Console.WriteLine(item);
                }
            }
        }
Beispiel #12
0
        private static void FunWithEntitySQL()
        {
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // Build a string containing Entity SQL syntax.
                string query = "SELECT VALUE car FROM AutoLotEntities.Cars AS car WHERE car.Color='black'";

                // Now build a ObjectQuery<T> based on the string.
                var blackCars = context.CreateQuery <Car>(query);

                foreach (var item in blackCars)
                {
                    Console.WriteLine(item);
                }
            }
        }
Beispiel #13
0
        private static void FunWIthEntitySQL()
        {
            using (AutoLotEntities context = new AutoLotEntities())
            {
                //  构建一个包含Entity SQL语法的字符串
                string query = "SELECT VALUE car FROM AutoLotEntities.Cars " +
                               "AS car WHERE car.Color='black'";

                //  现在基于该字符串构建一个ObjectQuery<T>
                var blackCars = context.CreateQuery <Car>(query);
                foreach (var item in blackCars)
                {
                    Console.WriteLine(item);
                }
            }
        }
Beispiel #14
0
        private static void UpdateRecord()
        {
            //  通过主键查找要更新的汽车
            using (AutoLotEntities context = new AutoLotEntities())
            {
                //  为查找的实体定义主键
                EntityKey key = new EntityKey("AutoLotEntities.Cars", "CarID", 2222);

                //  查找实体,如果存在的话将其删除
                Car carToUpdate = (Car)context.GetObjectByKey(key);
                if (carToUpdate != null)
                {
                    carToUpdate.Color = "Blue";
                    context.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Linq to Entity
        /// </summary>
        private static void FunWithEntitySQL()
        {
            using (AutoLotEntities context = new AutoLotEntities())
            {
                ObjectContext newContext = ((IObjectContextAdapter)context).ObjectContext;
                //构建一个包含Entity SQL语法的字符串
                string query = "SELECT VALUE car FROM AutoLotEntities.Cars " + "AS car WHERE car.Color = 'black'";


                var blackCars = newContext.CreateQuery <Car>(query);

                foreach (var item in blackCars)
                {
                    Console.WriteLine(item);
                }
            }
        }
Beispiel #16
0
        private static void RemoveRecord()
        {
            // Find a car to delete by primary key.
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // Define a key for the entity we are looking for.
                EntityKey key = new EntityKey("AutoLotEntities.Cars", "CarID", 2222);

                // See if we have it.
                Car carToDelete = (Car)context.GetObjectByKey(key);
                if (carToDelete != null)
                {
                    context.DeleteObject(carToDelete);
                    context.SaveChanges();
                }
            }
        }
Beispiel #17
0
        private static void UpdateRecord()
        {
            // Find a car to delete by primary key.
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // Define a key for the entity we are looking for.
                EntityKey key = new EntityKey("AutoLotEntities.Cars", "CarID", 2222);

                // Grab the car, change it, save!
                Car carToUpdate = (Car)context.GetObjectByKey(key);
                if (carToUpdate != null)
                {
                    carToUpdate.Color = "Blue";
                    context.SaveChanges();
                }
            }
        }
Beispiel #18
0
        private static void FunWithLINQQueries()
        {
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // Get all data from the Inventory table.
                // Could also write:
                // var allData = (from item in context.Cars select item).ToArray();
                var allData = context.Cars.ToArray();

                // Get a projection of new data.
                var colorsMakes = from item in allData select new { item.Color, item.Make };

                // Get only items where CarID < 1000.
                var idsLessThan1000 = from item in allData where
                                      item.CarID < 1000 select item;
            }
        }
Beispiel #19
0
 private static void AddNewRecord()
 {
     // Add record to the Inventory table of the AutoLot
     // database.
     using (AutoLotEntities context = new AutoLotEntities())
     {
         try
         {
             context.Cars.AddObject(new Car() { CarID = 2222, Make = "Yugo", Color = "Brown" });
             context.SaveChanges();
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.InnerException.Message);
         }
     }
 }
 private static void AddNewRecord()
 {
     using (AutoLotEntities context = new AutoLotEntities())
     {
         try
         {
             context.Cars.Add(new Car()
             {
                 CarID = 2222,
                 Make  = "Yugo",
                 Color = "Brown"
             });
             context.SaveChanges();
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.InnerException.Message);
         }
     }
 }
Beispiel #21
0
 private static void AddNewRecord()
 {
     // Add record to the Inventory table of the AutoLot
     // database.
     using (AutoLotEntities context = new AutoLotEntities())
     {
         try
         {
             context.Cars.AddObject(new Car()
             {
                 CarID = 2222, Make = "Yugo", Color = "Brown"
             });
             context.SaveChanges();
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.InnerException.Message);
         }
     }
 }
Beispiel #22
0
        private static void RemoveRecord()
        {
            //  通过主键查找要删除的汽车
            using (AutoLotEntities context = new AutoLotEntities())
            {
                //  为查找的实体定义主键
                //EntityKey key = new EntityKey("AutoLotEntity.Cars", "CarID", 2222);

                //  查找实体,如果存在的话将其删除
                //Car carToDelete = (Car)context.GetObjectByKey(key);
                var carToDelete = (from c in context.Cars
                                   where c.CarID == 2222
                                   select c).FirstOrDefault();
                if (carToDelete != null)
                {
                    context.DeleteObject(carToDelete);
                    context.SaveChanges();
                }
            }
        }
Beispiel #23
0
 private static void AddNewRecord()
 {
     //  向AutoLot数据库的Inventory表添加一条记录
     using (AutoLotEntities context = new AutoLotEntities())
     {
         try
         {
             //  对新的记录进行硬编码,仅供测试
             context.AddToCars(new Car()
             {
                 CarID = 2222,
                 Make  = "Yugo",
                 Color = "Brown"
             });
             context.SaveChanges();
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.InnerException.Message);
         }
     }
 }
Beispiel #24
0
        private static void AddNewRecord() {
            using (AutoLotEntities context = new AutoLotEntities()) {
                try {
                    context.Cars.Add(new Car() { CarID = 2224, Make = "Yugo", Color = "Brown" });
                    context.SaveChanges();
                    Console.WriteLine("added 2224");
                } catch (DbUpdateException ex) {
                    Exception e = ex;
                    Console.WriteLine(ex == ex.InnerException);
                    while (e != null) {
                        Console.WriteLine(e.Message);
                        e = e.InnerException;
                    }
                    
                }
                
                catch (Exception ex) {
                    Console.WriteLine(ex);
                }

            }
        }
Beispiel #25
0
        private static void FunWithLINQQueries()
        {
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // Get all data from the Inventory table.
                // could also write:
                // var allData = (from item in context.Cars select item).ToArray();
                var allData = context.Cars.ToArray();

                // Get a projection of new data.
                var colorsMakes = from item in allData select new { item.Color, item.Make };
                foreach (var item in colorsMakes)
                {
                    Console.WriteLine(item);
                }

                // Get only items where CarID < 1000
                var idsLessThan1000 = from item in allData where item.CarID < 1000 select item;
                foreach (var item in idsLessThan1000)
                {
                    Console.WriteLine(item);
                }
            }
        }
Beispiel #26
0
        private static void UpdateRecord()
        {
            // Find a car to delete by primary key.
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // Define a key for the entity we are looking for.
                EntityKey key = new EntityKey("AutoLotEntities.Cars", "CarID", 2222);

                // Grab the car, change it, save!
                Car carToUpdate = (Car)context.GetObjectByKey(key);
                Console.WriteLine(carToUpdate.EntityState);
                if (carToUpdate != null)
                {
                    carToUpdate.Color = "Blue";
                    context.SaveChanges();
                }
            }
        }
Beispiel #27
0
        private static void RemoveRecord()
        {
            // Find a car to delete by primary key.
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // Define a key for the entity we are looking for.
                EntityKey key = new EntityKey("AutoLotEntities.Cars", "CarID", 2222);

                // See if we have it.
                Car carToDelete = (Car)context.GetObjectByKey(key);
                if (carToDelete != null)
                {
                    context.DeleteObject(carToDelete);
                    context.SaveChanges();
                }
            }
        }
Beispiel #28
0
        private static void FunWithLINQQUeries() {
            using (AutoLotEntities context = new AutoLotEntities()) {
                var colorMakes = from item in context.Cars
                                 select new { item.Color, item.Make };
                foreach (var item in colorMakes) {
                    Console.WriteLine(item);
                }

                var idsLessThan1000 = from item in context.Cars
                                      where item.CarID < 1000
                                      select item;
                foreach (var item in idsLessThan1000) {
                    Console.WriteLine(item);
                }
            }
        }
Beispiel #29
0
        private static void RemoveRecordWithLINQ()
        {
            // Find a car to delete by primary key.
            using (AutoLotEntities context = new AutoLotEntities())
            {
                // See if we have it.
                var carToDelete = (from c in context.Cars where c.CarID == 2222 select c).FirstOrDefault();

                if (carToDelete != null)
                {
                    context.DeleteObject(carToDelete);
                    context.SaveChanges();
                }
            }
        }
Beispiel #30
0
 private static void FunWithEntitySQL() {
     using (AutoLotEntities context = new AutoLotEntities()) {
         Console.WriteLine(context.Cars.ToString());
         string queryString = "SELECT VALUE car from AutoLotEntities.Cars as car WHERE car.Color='Black'";
         ObjectQuery<Car> contactQuery = new ObjectQuery<Car>(queryString, ((IObjectContextAdapter)context).ObjectContext);            
         foreach (var item in contactQuery) {
             Console.WriteLine(item);
         }
     }
 }
Beispiel #31
0
 private static void PrintAllInventory()
 {
     // Select all items from the Inventory table of AutoLot,
     // and print out the data using our custom ToString()
     // of the Car entity class.
     using (AutoLotEntities context = new AutoLotEntities())
     {
         foreach (Car c in context.Cars)
             Console.WriteLine(c);
     }
 }