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(); } } }
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(); } } }
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(); } } }
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(); } } }
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(); } } }