Пример #1
0
 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();
                }
            }
        }
Пример #4
0
 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.
     }
 }
Пример #5
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();
                }
            }
        }
Пример #6
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();
                }
            }
        }
Пример #7
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();
                }
            }
        }
Пример #8
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();
                }
            }
        }
Пример #9
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);
         }
     }
 }
Пример #11
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);
         }
     }
 }
Пример #12
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();
                }
            }
        }
Пример #13
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);
         }
     }
 }
Пример #14
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);
                }

            }
        }
Пример #15
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();
                }
            }
        }
Пример #16
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();
                }
            }
        }
Пример #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);
                Console.WriteLine(carToUpdate.EntityState);
                if (carToUpdate != null)
                {
                    carToUpdate.Color = "Blue";
                    context.SaveChanges();
                }
            }
        }