public void LogMovement(DeviceMovementDto movement) { var device = DataContext.Devices.FirstOrDefault(d => d.DeviceName == movement.DeviceName); //if device is notifying movement for the first time, persist device to database if (device == null) { device = new Device { DeviceName = movement.DeviceName }; DataContext.Devices.Add(device); } //save changes just for sure, maybe not needed var deviceId = DataContext.SaveChanges(); device = DataContext.Devices.Find(deviceId); //Convert dto to db entity var domainObject = movement.MapDomainObject(); //Set device dependency domainObject.Device = device; //persist movement DataContext.DeviceMovements.Add(domainObject); }
public static DeviceMovement MapDomainObject(this DeviceMovementDto dto) => new DeviceMovement { DeviceName = dto.DeviceName, StartCoordinate = dto.StartCoordinate.MapDomainObject(), EndCoordinate = dto.EndCoordinate.MapDomainObject(), AverageVelocityMetersPerSecond = dto.AverageVelocityMetersPerSecond, TimeRecorded = dto.TimeRecorded };