public List <GenerationTypeFuelForeign> ExtractGenerationTypeFuelsForeign(string oracleConnString) { using (OracleConnection con = new OracleConnection(oracleConnString)) { using (OracleCommand cmd = con.CreateCommand()) { try { con.Open(); cmd.BindByName = true; cmd.CommandText = "select ID, TYPE_ID, FUEL_ID from TYPE_FUEL_RELATION where :id=1 and TYPE_ID is NOT NULL and FUEL_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 <GenerationTypeFuelForeign> genTypeFuelsForeign = new List <GenerationTypeFuelForeign>(); while (reader.Read()) { GenerationTypeFuelForeign genTypeFuelForeign = new GenerationTypeFuelForeign(); genTypeFuelForeign.WebUatId = reader.GetInt32(0); genTypeFuelForeign.GenerationTypeWebUatId = reader.GetInt32(1); genTypeFuelForeign.FuelWebUatId = reader.GetInt32(2); genTypeFuelsForeign.Add(genTypeFuelForeign); } reader.Dispose(); return(genTypeFuelsForeign); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } } } }
public async Task <GenerationTypeFuel> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, GenerationTypeFuelForeign genTypeFuelForeign, EntityWriteOption opt) { // check if entity already exists GenerationTypeFuel existingGenTypeFuel = await _context.GenerationTypeFuels.SingleOrDefaultAsync(gtf => gtf.WebUatId == genTypeFuelForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingGenTypeFuel != null) { return(existingGenTypeFuel); } // find the GenerationType via the Substation WebUatId int genTypeWebUatId = genTypeFuelForeign.GenerationTypeWebUatId; GenerationType genType = await _context.GenerationTypes.SingleOrDefaultAsync(gt => gt.WebUatId == genTypeWebUatId); // if GenerationType doesnot exist, skip the import. Ideally, there should not be such case if (genType == null) { _log.LogCritical($"Unable to find GenerationType with webUatId {genTypeWebUatId} while inserting GenerationTypeFuel with webUatId {genTypeFuelForeign.WebUatId}"); return(null); } // find the Fuel via the Fuel WebUatId int fuelWebUatId = genTypeFuelForeign.FuelWebUatId; Fuel fuel = await _context.Fuels.SingleOrDefaultAsync(f => f.WebUatId == fuelWebUatId); // if fuel doesnot exist, skip the import. Ideally, there should not be such case if (fuel == null) { _log.LogCritical($"Unable to find Fuel with webUatId {fuelWebUatId} while inserting GenerationTypeFuel with webUatId {genTypeFuelForeign.WebUatId}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingGenTypeFuel != null) { _context.GenerationTypeFuels.Remove(existingGenTypeFuel); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingGenTypeFuel == null || (opt == EntityWriteOption.Replace && existingGenTypeFuel != null)) { GenerationTypeFuel newGenTypeFuel = new GenerationTypeFuel(); newGenTypeFuel.FuelId = fuel.FuelId; newGenTypeFuel.GenerationTypeId = genType.GenerationTypeId; newGenTypeFuel.WebUatId = genTypeFuelForeign.WebUatId; _context.GenerationTypeFuels.Add(newGenTypeFuel); await _context.SaveChangesAsync(); return(newGenTypeFuel); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingGenTypeFuel != null) { existingGenTypeFuel.FuelId = fuel.FuelId; existingGenTypeFuel.GenerationTypeId = genType.GenerationTypeId; await _context.SaveChangesAsync(); return(existingGenTypeFuel); } return(null); }