public List <BusReactorForeign> ExtractBusReactorsForeign(string oracleConnString) { using (OracleConnection con = new OracleConnection(oracleConnString)) { using (OracleCommand cmd = con.CreateCommand()) { try { con.Open(); cmd.BindByName = true; cmd.CommandText = @"select ID, REACTOR_NO, FK_BUS, FK_SUBSTATION, MVAR_CAPACITY, DATETIME_OF_COMMISSIONING, DATETIME_OF_COD, DATETIME_OF_DECOMMISSIONING, LOCATION_ID, REACTOR_NAME from REPORTING_WEB_UI_UAT.BUS_REACTOR where :id=1"; // Assign id parameter OracleParameter id = new OracleParameter("id", 1); cmd.Parameters.Add(id); //Execute the command and use DataReader to display the data OracleDataReader reader = cmd.ExecuteReader(); List <BusReactorForeign> brsForeign = new List <BusReactorForeign>(); while (reader.Read()) { BusReactorForeign brForeign = new BusReactorForeign(); brForeign.WebUatId = reader.GetInt32(0); brForeign.BusReactorNumber = reader.GetInt32(1); brForeign.BusWebUatId = reader.GetInt32(2); brForeign.SubstationWebUatId = reader.GetInt32(3); brForeign.MvarCapacity = reader.GetDecimal(4); brForeign.CommDate = reader.GetDateTime(5); brForeign.CodDate = reader.GetDateTime(6); brForeign.DecommDate = reader.GetDateTime(7); brForeign.StateWebUatId = reader.GetInt32(8); brForeign.Name = reader.GetString(9); brsForeign.Add(brForeign); } reader.Dispose(); return(brsForeign); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } } } }
public async Task <BusReactor> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, BusReactorForeign brForeign, EntityWriteOption opt) { // check if entity already exists BusReactor existingBr = await _context.BusReactors.SingleOrDefaultAsync(br => br.WebUatId == brForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingBr != null) { return(existingBr); } // find the Substation of the BusReactor via the Substation WebUatId int ssWebUatId = brForeign.SubstationWebUatId; Substation substation = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == ssWebUatId); // if Substation doesnot exist, skip the import. Ideally, there should not be such case if (substation == null) { _log.LogCritical($"Unable to find Substation with webUatId {ssWebUatId} while inserting BusReactor with webUatId {brForeign.WebUatId} and name {brForeign.Name}"); return(null); } // find the Bus of the BusReactor via the Bus WebUatId int busWebUatId = brForeign.BusWebUatId; Bus bus = await _context.Buses.SingleOrDefaultAsync(b => b.WebUatId == busWebUatId); // if Bus doesnot exist, skip the import. Ideally, there should not be such case if (bus == null) { _log.LogCritical($"Unable to find Bus with webUatId {busWebUatId} while inserting BusReactor with webUatId {brForeign.WebUatId} and name {brForeign.Name}"); return(null); } // find the State of the BusReactor via the State WebUatId int stateWebUatId = brForeign.StateWebUatId; State state = await _context.States.SingleOrDefaultAsync(s => s.WebUatId == stateWebUatId); // if state doesnot exist, skip the import. Ideally, there should not be such case if (state == null) { _log.LogCritical($"Unable to find State with webUatId {stateWebUatId} while inserting Transformer with webUatId {brForeign.WebUatId} and name {brForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingBr != null) { _context.BusReactors.Remove(existingBr); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingBr == null || (opt == EntityWriteOption.Replace && existingBr != null)) { BusReactor newBr = new BusReactor(); newBr.Name = brForeign.Name; newBr.BusReactorNumber = brForeign.BusReactorNumber; newBr.MvarCapacity = brForeign.MvarCapacity; newBr.CommDate = brForeign.CommDate; newBr.CodDate = brForeign.CodDate; newBr.DecommDate = brForeign.DecommDate; newBr.BusId = bus.BusId; newBr.SubstationId = substation.SubstationId; newBr.StateId = substation.StateId; newBr.WebUatId = brForeign.WebUatId; _context.BusReactors.Add(newBr); await _context.SaveChangesAsync(); return(newBr); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingBr != null) { existingBr.Name = brForeign.Name; existingBr.BusReactorNumber = brForeign.BusReactorNumber; existingBr.MvarCapacity = brForeign.MvarCapacity; existingBr.CommDate = brForeign.CommDate; existingBr.CodDate = brForeign.CodDate; existingBr.DecommDate = brForeign.DecommDate; existingBr.BusId = bus.BusId; existingBr.SubstationId = substation.SubstationId; existingBr.StateId = substation.StateId; await _context.SaveChangesAsync(); return(existingBr); } return(null); }