public void SaveMetering(IMetering metering) { using (var dbContextTransaction = _context.Database.BeginTransaction()) { try { var m = _context.Meterings.Add(Mapper.Map <MeteringDAO>(metering)); _context.SaveChanges(); foreach (var met in metering.SensorValues) { var sv = _context.SensorValues.Add(new SensorValueDAO() { Value = DAOHelper.ObjectToByteArray(met.Value) }); _context.SaveChanges(); _context.MeteringSensorRelations.Add(new MeteringSensorValueRelationDAO() { MeteringId = m.Id, SensorValueId = sv.Id, PropertyName = met.Key.Name }); _context.SaveChanges(); } dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); throw; } } }