public static void EnsureSeedData(this PowerContext context) { if (!context.Database.GetPendingMigrations().Any()) { if (!context.Usinas.Any()) { context.Usinas.AddRange(AddUsinas()); } if (!context.Agentes.Any()) { context.Agentes.AddRange(AddAgentes()); } context.SaveChanges(); if (!context.Equipamentos.Any()) { context.Equipamentos.AddRange(AddEquipamentos()); } context.SaveChanges(); } }
public IActionResult Post([FromBody] MeasurePoint MeasurePoint) { //MeasurePoint MeasurePoint = JsonConvert.DeserializeObject<MeasurePoint>(JsonString); using (PowerContext db = new PowerContext()) { if (MeasurePoint.ElectricMeter.MeterType != null && MeasurePoint.VoltageTransformer.VTType != null && MeasurePoint.CurrentTransformer.CTType != null) { var ExistingMeterName = db.MeterTypes.SingleOrDefault(p => p.Title == MeasurePoint.ElectricMeter.MeterType.Title); if (ExistingMeterName != null) { MeasurePoint.ElectricMeter.MeterType = ExistingMeterName; } else { db.MeterTypes.Add(MeasurePoint.ElectricMeter.MeterType); } var ExistingVTName = db.VTTypes.SingleOrDefault(p => p.Title == MeasurePoint.VoltageTransformer.VTType.Title); if (ExistingVTName != null) { MeasurePoint.VoltageTransformer.VTType = ExistingVTName; } else { db.VTTypes.Add(MeasurePoint.VoltageTransformer.VTType); } var ExistingCTName = db.CTTypes.SingleOrDefault(p => p.Title == MeasurePoint.CurrentTransformer.CTType.Title); if (ExistingCTName != null) { MeasurePoint.CurrentTransformer.CTType = ExistingCTName; } else { db.CTTypes.Add(MeasurePoint.CurrentTransformer.CTType); } db.VoltageTransformers.Add(MeasurePoint.VoltageTransformer); db.CurrentTransformers.Add(MeasurePoint.CurrentTransformer); db.ElectricMeters.Add(MeasurePoint.ElectricMeter); db.MeasurePoints.Add(MeasurePoint); db.SaveChanges(); } return(Ok(MeasurePoint)); } }
public virtual T Add(T t) { _context.Set <T>().Add(t); _context.SaveChanges(); return(t); }