public async Task ImportForeignHvdcLineCkts(WRLDCWarehouseDbContext _context, ILogger _log, string oracleConnStr, EntityWriteOption opt) { HvdcLineCktExtract hvdcLineCktExtract = new HvdcLineCktExtract(); List <HvdcLineCktForeign> hvdcLineCktForeignList = hvdcLineCktExtract.ExtractHvdcLineCktForeign(oracleConnStr); LoadHvdcLineCkt loadHvdcLineCkt = new LoadHvdcLineCkt(); foreach (HvdcLineCktForeign cktForeign in hvdcLineCktForeignList) { HvdcLineCkt insertedCkt = await loadHvdcLineCkt.LoadSingleAsync(_context, _log, cktForeign, opt); } }
public async Task <HvdcLineCkt> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, HvdcLineCktForeign hvdcLineCktForeign, EntityWriteOption opt) { // check if entity already exists HvdcLineCkt existingHvdcLineCkt = await _context.HvdcLineCkts.SingleOrDefaultAsync(ckt => ckt.WebUatId == hvdcLineCktForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingHvdcLineCkt != null) { return(existingHvdcLineCkt); } // find the HvdcLine of the HvdcLineCkt via the State hvdcLineWebUatId int hvdcLineWebUatId = hvdcLineCktForeign.HvdcLineWebUatId; HvdcLine hvdcLine = await _context.HvdcLines.SingleOrDefaultAsync(hl => hl.WebUatId == hvdcLineWebUatId); // if HvdcLine doesnot exist, skip the import. Ideally, there should not be such case if (hvdcLine == null) { _log.LogCritical($"Unable to find HvdcLine with webUatId {hvdcLineWebUatId} while inserting HvdcLineCkt with webUatId {hvdcLineCktForeign.WebUatId} and name {hvdcLineCktForeign.Name}"); return(null); } // find the FromBus of the HvdcLineCkt via the FromBusWebUatId int fromBusWebUatId = hvdcLineCktForeign.FromBusWebUatId; Bus fromBus = await _context.Buses.SingleOrDefaultAsync(b => b.WebUatId == fromBusWebUatId); // if FromBus doesnot exist, skip the import. Ideally, there should not be such case if (fromBus == null) { _log.LogCritical($"Unable to find FromBus with webUatId {fromBusWebUatId} while inserting HvdcLineCkt with webUatId {hvdcLineCktForeign.WebUatId} and name {hvdcLineCktForeign.Name}"); return(null); } // find the TomBus of the HvdcLineCkt via the ToBusWebUatId int toBusWebUatId = hvdcLineCktForeign.ToBusWebUatId; Bus toBus = await _context.Buses.SingleOrDefaultAsync(b => b.WebUatId == toBusWebUatId); // if ToBus doesnot exist, skip the import. Ideally, there should not be such case if (toBus == null) { _log.LogCritical($"Unable to find ToBus with webUatId {toBusWebUatId} while inserting HvdcLineCkt with webUatId {hvdcLineCktForeign.WebUatId} and name {hvdcLineCktForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingHvdcLineCkt != null) { _context.HvdcLineCkts.Remove(existingHvdcLineCkt); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingHvdcLineCkt == null || (opt == EntityWriteOption.Replace && existingHvdcLineCkt != null)) { HvdcLineCkt newHvdcLineCkt = new HvdcLineCkt(); newHvdcLineCkt.Name = hvdcLineCktForeign.Name; newHvdcLineCkt.CktNumber = hvdcLineCktForeign.CktNumber.ToString(); newHvdcLineCkt.HvdcLineId = hvdcLine.HvdcLineId; newHvdcLineCkt.FromBusId = fromBus.BusId; newHvdcLineCkt.ToBusId = toBus.BusId; newHvdcLineCkt.NumConductorsPerCkt = hvdcLineCktForeign.NumConductorsPerCkt; newHvdcLineCkt.Length = hvdcLineCktForeign.Length; newHvdcLineCkt.ThermalLimitMVA = hvdcLineCktForeign.ThermalLimitMVA; newHvdcLineCkt.FtcDate = hvdcLineCktForeign.FtcDate; newHvdcLineCkt.TrialOperationDate = hvdcLineCktForeign.TrialOperationDate; newHvdcLineCkt.CommDate = hvdcLineCktForeign.CommDate; newHvdcLineCkt.CodDate = hvdcLineCktForeign.CodDate; newHvdcLineCkt.DeCommDate = hvdcLineCktForeign.DeCommDate; newHvdcLineCkt.WebUatId = hvdcLineCktForeign.WebUatId; _context.HvdcLineCkts.Add(newHvdcLineCkt); await _context.SaveChangesAsync(); return(newHvdcLineCkt); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingHvdcLineCkt != null) { existingHvdcLineCkt.Name = hvdcLineCktForeign.Name; existingHvdcLineCkt.CktNumber = hvdcLineCktForeign.CktNumber.ToString(); existingHvdcLineCkt.HvdcLineId = hvdcLine.HvdcLineId; existingHvdcLineCkt.FromBusId = fromBus.BusId; existingHvdcLineCkt.ToBusId = toBus.BusId; existingHvdcLineCkt.NumConductorsPerCkt = hvdcLineCktForeign.NumConductorsPerCkt; existingHvdcLineCkt.Length = hvdcLineCktForeign.Length; existingHvdcLineCkt.ThermalLimitMVA = hvdcLineCktForeign.ThermalLimitMVA; existingHvdcLineCkt.FtcDate = hvdcLineCktForeign.FtcDate; existingHvdcLineCkt.TrialOperationDate = hvdcLineCktForeign.TrialOperationDate; existingHvdcLineCkt.CommDate = hvdcLineCktForeign.CommDate; existingHvdcLineCkt.CodDate = hvdcLineCktForeign.CodDate; existingHvdcLineCkt.DeCommDate = hvdcLineCktForeign.DeCommDate; await _context.SaveChangesAsync(); return(existingHvdcLineCkt); } return(null); }
public async Task <HvdcLineCktOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, HvdcLineCktOwnerForeign cktOwnerForeign, EntityWriteOption opt) { // check if entity already exists HvdcLineCktOwner existingCktOwner = await _context.HvdcLineCktOwners.SingleOrDefaultAsync(cO => cO.WebUatId == cktOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingCktOwner != null) { return(existingCktOwner); } // find the HvdcLineCkt via the HvdcLineCkt WebUatId int cktWebUatId = cktOwnerForeign.HvdcLineCktWebUatId; HvdcLineCkt ckt = await _context.HvdcLineCkts.SingleOrDefaultAsync(c => c.WebUatId == cktWebUatId); // if HvdcLineCkt doesnot exist, skip the import. Ideally, there should not be such case if (ckt == null) { _log.LogCritical($"Unable to find HvdcLineCkt with webUatId {cktWebUatId} while inserting HvdcLineCktOwner with webUatId {cktOwnerForeign.WebUatId}"); return(null); } // find the Owner of the HvdcLineCkt via the Owner WebUatId int ownerWebUatId = cktOwnerForeign.OwnerWebUatId; Owner owner = await _context.Owners.SingleOrDefaultAsync(o => o.WebUatId == ownerWebUatId); // if owner doesnot exist, skip the import. Ideally, there should not be such case if (owner == null) { _log.LogCritical($"Unable to find Owner with webUatId {ownerWebUatId} while inserting HvdcLineCktOwner with webUatId {cktOwnerForeign.WebUatId}"); return(null); } try { // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingCktOwner != null) { _context.HvdcLineCktOwners.Remove(existingCktOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingCktOwner == null || (opt == EntityWriteOption.Replace && existingCktOwner != null)) { HvdcLineCktOwner newCktOwner = new HvdcLineCktOwner(); newCktOwner.OwnerId = owner.OwnerId; newCktOwner.HvdcLineCktId = ckt.HvdcLineCktId; newCktOwner.WebUatId = cktOwnerForeign.WebUatId; _context.HvdcLineCktOwners.Add(newCktOwner); await _context.SaveChangesAsync(); return(newCktOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingCktOwner != null) { existingCktOwner.OwnerId = owner.OwnerId; existingCktOwner.HvdcLineCktId = ckt.HvdcLineCktId; await _context.SaveChangesAsync(); return(existingCktOwner); } } catch (DbUpdateException e) { _log.LogCritical($"Error occured while inserting HvdcLineCktOwner with webUatId {cktOwnerForeign.WebUatId}, owner id {owner.OwnerId} and cktId {ckt.HvdcLineCktId}"); _log.LogCritical($"EntityWriteOption = {opt.ToString()}"); _log.LogCritical($"{e.Message}"); return(null); } return(null); }