public override bool Apply(DiscoDataContext Database, Device Device) { if (this.FieldAction == EntityState.Added || this.FieldAction == EntityState.Modified) { DeviceDetail detail = Database.DeviceDetails.FirstOrDefault(dd => dd.DeviceSerialNumber == Device.SerialNumber && dd.Scope == DeviceDetail.ScopeHardware && dd.Key == DeviceDetail.HardwareKeyLanMacAddress); if (detail == null) { detail = new DeviceDetail() { Device = Device, DeviceSerialNumber = Device.SerialNumber, Scope = DeviceDetail.ScopeHardware, Key = DeviceDetail.HardwareKeyLanMacAddress }; Database.DeviceDetails.Add(detail); } detail.Value = parsedValue; return true; } else { return false; } }
private static void SetDetail(this Device device, string Scope, string Key, string Value) { if (device == null) throw new ArgumentNullException("device"); if (string.IsNullOrEmpty(Scope)) throw new ArgumentNullException("Scope"); if (string.IsNullOrEmpty(Key)) throw new ArgumentNullException("Key"); var detail = device.DeviceDetails.Where(d => d.Scope == Scope && d.Key == Key).FirstOrDefault(); // No Detail Stored & Set to Null if (detail == null && Value == null) return; if (detail == null) { detail = new DeviceDetail() { DeviceSerialNumber = device.SerialNumber, Scope = Scope, Key = Key, Value = Value }; device.DeviceDetails.Add(detail); } if (detail.Value != Value) { if (Value == null) { device.DeviceDetails.Remove(detail); } else { detail.Value = Value; } } }