public List <HvdcPoleOwnerForeign> ExtractHvdcPoleOwnersForeign(string oracleConnString) { using (OracleConnection con = new OracleConnection(oracleConnString)) { using (OracleCommand cmd = con.CreateCommand()) { try { con.Open(); cmd.BindByName = true; cmd.CommandText = @"SELECT ID, PARENT_ENTITY_ATTRIBUTE_ID, CHILD_ENTITY_ATTRIBUTE_ID from REPORTING_WEB_UI_UAT.ENTITY_ENTITY_RELN WHERE PARENT_ENTITY='HVDC_POLE' and PARENT_ENTITY_ATTRIBUTE='Owner' and CHILD_ENTITY='OWNER' and CHILD_ENTITY_ATTRIBUTE='OwnerId'"; // 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 <HvdcPoleOwnerForeign> poleOwnersForeign = new List <HvdcPoleOwnerForeign>(); while (reader.Read()) { HvdcPoleOwnerForeign poleOwnerForeign = new HvdcPoleOwnerForeign(); poleOwnerForeign.WebUatId = reader.GetInt32(0); poleOwnerForeign.HvdcPoleWebUatId = reader.GetInt32(1); poleOwnerForeign.OwnerWebUatId = reader.GetInt32(2); poleOwnersForeign.Add(poleOwnerForeign); } reader.Dispose(); return(poleOwnersForeign); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } } } }
public async Task <HvdcPoleOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, HvdcPoleOwnerForeign poleOwnerForeign, EntityWriteOption opt) { // check if entity already exists HvdcPoleOwner existingPoleOwner = await _context.HvdcPoleOwners.SingleOrDefaultAsync(cO => cO.WebUatId == poleOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingPoleOwner != null) { return(existingPoleOwner); } // find the HvdcPole via the HvdcPole WebUatId int poleWebUatId = poleOwnerForeign.HvdcPoleWebUatId; HvdcPole pole = await _context.HvdcPoles.SingleOrDefaultAsync(c => c.WebUatId == poleWebUatId); // if HvdcPole doesnot exist, skip the import. Ideally, there should not be such case if (pole == null) { _log.LogCritical($"Unable to find HvdcPole with webUatId {poleWebUatId} while inserting HvdcPoleOwner with webUatId {poleOwnerForeign.WebUatId}"); return(null); } // find the Owner of the Pole via the Owner WebUatId int ownerWebUatId = poleOwnerForeign.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 HvdcPoleOwner with webUatId {poleOwnerForeign.WebUatId}"); return(null); } try { // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingPoleOwner != null) { _context.HvdcPoleOwners.Remove(existingPoleOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingPoleOwner == null || (opt == EntityWriteOption.Replace && existingPoleOwner != null)) { HvdcPoleOwner newPoleOwner = new HvdcPoleOwner(); newPoleOwner.OwnerId = owner.OwnerId; newPoleOwner.HvdcPoleId = pole.HvdcPoleId; newPoleOwner.WebUatId = poleOwnerForeign.WebUatId; _context.HvdcPoleOwners.Add(newPoleOwner); await _context.SaveChangesAsync(); return(newPoleOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingPoleOwner != null) { existingPoleOwner.OwnerId = owner.OwnerId; existingPoleOwner.HvdcPoleId = pole.HvdcPoleId; await _context.SaveChangesAsync(); return(existingPoleOwner); } } catch (DbUpdateException e) { _log.LogCritical($"Error occured while inserting HvdcPoleOwner with webUatId {poleOwnerForeign.WebUatId}, owner id {owner.OwnerId} and poleId {pole.HvdcPoleId}"); _log.LogCritical($"EntityWriteOption = {opt.ToString()}"); _log.LogCritical($"{e.Message}"); return(null); } return(null); }