public void Delete(DeviceModel instance) { using (var db = new TestHXContext()) { db.Entry <DeviceModel>(instance).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } }
public void Delete(DeviceTypeModel instance) { using (var db = new TestHXContext()) { DeviceTypeModel dtm = db.DeviceType.Find(instance.Id); db.Entry <DeviceTypeModel>(dtm).State = System.Data.Entity.EntityState.Deleted; //获取关联的设备并更新外键为Null var d = db.Device.Where(a => a.TypeId == instance.Id); foreach (var item in d) { item.TypeId = null; } db.SaveChanges(); } }