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