public static ProductionOrder Add(string uniqueId, string customerName, int meterCount, string paramSetName, int initialSerialNo)
        {
            using (var context = ApplicationDbContext.Create())
            {
                if (GetOneByUniqueId(uniqueId) != null)
                {
                    throw new InvalidOperationException("Order with same unique key already exists");
                }

                // find and load parameter set
                var paramSet = ParameterInfoSetRepository.GetOneByName(paramSetName, initialSerialNo);
                if (paramSet == null)
                {
                    throw new NullReferenceException("Parameter set not found");
                }

                var order = new ProductionOrder
                                {
                                    CreatedAt = DateTime.Now,
                                    UniqueId = uniqueId,
                                    CustomerName = customerName,
                                    MeterCount = meterCount,
                                    ParameterInfoSetName = paramSet.Name,
                                    Batches = new List<ProductionBatch>(),
                                    InitialMeterSerialNo = initialSerialNo
                                };

                context.ProductionOrders.Add(order);
                context.SaveChanges();

                return order;
            }
        }
        public static bool Delete(ProductionOrder order)
        {
            using (var context = ApplicationDbContext.Create())
            {
                var existing = context.ProductionOrders
                                .Include(o => o.Batches.Select(b => b.Meters.Select(m => m.Parameters)))
                                .FirstOrDefault(or => or.ProductionOrderId == order.ProductionOrderId);
                if (existing == null)
                {
                    return false;
                }

                context.ProductionOrders.Remove(existing);

                context.SaveChanges();
                return true;
            }
        }
 private static void CreateNewParametersFromInfoSet(ProductionOrder order, List<Parameter> parameterList)
 {
     var orderInfoSet = ParameterInfoSetRepository.GetOneByName(order.ParameterInfoSetName);
     orderInfoSet.ParameterInfos.ForEach(
         p =>
             parameterList.Add(
                 new Parameter()
                 {
                     Name = p.Name,
                     ObisCode = p.ObisCode,
                     TagId = p.TagId,
                     IsActive = p.IsActive,
                     IsAutoIncrement = p.IsAutoIncrement,
                     IsPartOfMatrixCode = p.IsPartOfMatrixCode,
                     IsReadOnly = p.IsReadOnly,
                     IsNameplateOnly = p.IsNameplateOnly,
                     IsRandomTargetValue = p.IsRandomTargetValue,
                     IsWriteOnly = p.IsWriteOnly,
                     TargetValue = p.InitialValue,
                     CreatedAt = DateTime.Now,
                     ModifiedAt = new DateTime(1900, 1, 1),
                 }));
 }