public List <MajorSubstationForeign> ExtractMajorSubstationsForeign(string oracleConnString) { using (OracleConnection con = new OracleConnection(oracleConnString)) { using (OracleCommand cmd = con.CreateCommand()) { try { con.Open(); cmd.BindByName = true; cmd.CommandText = "select ID, NAME, STATE_ID from MAJOR_SUBSTATION where :id=1 and NAME IS NOT NULL and ID IS NOT NULL and STATE_ID IS NOT NULL"; // 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 <MajorSubstationForeign> majorSubstationsForeign = new List <MajorSubstationForeign>(); while (reader.Read()) { MajorSubstationForeign majorSSForeign = new MajorSubstationForeign(); majorSSForeign.WebUatId = reader.GetInt32(0); majorSSForeign.Name = reader.GetString(1); majorSSForeign.StateWebUatId = reader.GetInt32(2); majorSubstationsForeign.Add(majorSSForeign); } reader.Dispose(); return(majorSubstationsForeign); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } } } }
public async Task <MajorSubstation> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, MajorSubstationForeign majorSSForeign, EntityWriteOption opt) { // check if entity already exists MajorSubstation existingMajorSS = await _context.MajorSubstations.SingleOrDefaultAsync(mss => mss.WebUatId == majorSSForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingMajorSS != null) { return(existingMajorSS); } // find the region of the state via the region WebUatId int stateWebUatId = majorSSForeign.StateWebUatId; State majorSSState = await _context.States.SingleOrDefaultAsync(s => s.WebUatId == stateWebUatId); // if state doesnot exist, skip the import. Ideally, there should not be such case if (majorSSState == null) { _log.LogCritical($"Unable to find State with webUatId {stateWebUatId} while inserting MajorSubstation with webUatId {majorSSForeign.WebUatId} and name {majorSSForeign.Name}"); return(null); } // if entity is not present, then insert if (existingMajorSS == null) { MajorSubstation newMajorSS = new MajorSubstation(); newMajorSS.Name = majorSSForeign.Name; newMajorSS.StateId = majorSSState.StateId; newMajorSS.WebUatId = majorSSForeign.WebUatId; _context.MajorSubstations.Add(newMajorSS); await _context.SaveChangesAsync(); return(newMajorSS); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingMajorSS != null) { _context.MajorSubstations.Remove(existingMajorSS); MajorSubstation newMajorSS = new MajorSubstation(); newMajorSS.Name = majorSSForeign.Name; newMajorSS.StateId = majorSSState.StateId; newMajorSS.WebUatId = majorSSForeign.WebUatId; _context.MajorSubstations.Add(newMajorSS); await _context.SaveChangesAsync(); return(newMajorSS); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingMajorSS != null) { existingMajorSS.Name = majorSSForeign.Name; existingMajorSS.StateId = majorSSState.StateId; await _context.SaveChangesAsync(); return(existingMajorSS); } return(null); }