public List <SubstationOwnerForeign> ExtractSubstationOwnersForeign(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 ENTITY_ENTITY_RELN where :id=1 and PARENT_ENTITY='ASSOCIATE_SUBSTATION' and CHILD_ENTITY='OWNER' and PARENT_ENTITY_ATTRIBUTE='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 <SubstationOwnerForeign> ssOwnersForeign = new List <SubstationOwnerForeign>(); while (reader.Read()) { SubstationOwnerForeign ssOwnerForeign = new SubstationOwnerForeign(); ssOwnerForeign.WebUatId = reader.GetInt32(0); ssOwnerForeign.SubstationWebUatId = reader.GetInt32(1); ssOwnerForeign.OwnerWebUatId = reader.GetInt32(2); ssOwnersForeign.Add(ssOwnerForeign); } reader.Dispose(); return(ssOwnersForeign); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } } } }
public async Task <SubstationOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, SubstationOwnerForeign ssOwnerForeign, EntityWriteOption opt) { // check if entity already exists SubstationOwner existingSSOwner = await _context.SubstationOwners.SingleOrDefaultAsync(ssO => ssO.WebUatId == ssOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingSSOwner != null) { return(existingSSOwner); } // find the Substation via the Substation WebUatId int ssWebUatId = ssOwnerForeign.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 SubstationOwner with webUatId {ssOwnerForeign.WebUatId}"); return(null); } // find the Owner of the substation via the Owner WebUatId int ownerWebUatId = ssOwnerForeign.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 SubstationOwner with webUatId {ssOwnerForeign.WebUatId}"); return(null); } try { // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingSSOwner != null) { _context.SubstationOwners.Remove(existingSSOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingSSOwner == null || (opt == EntityWriteOption.Replace && existingSSOwner != null)) { SubstationOwner newSSOwner = new SubstationOwner(); newSSOwner.OwnerId = owner.OwnerId; newSSOwner.SubstationId = substation.SubstationId; newSSOwner.WebUatId = ssOwnerForeign.WebUatId; _context.SubstationOwners.Add(newSSOwner); await _context.SaveChangesAsync(); return(newSSOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingSSOwner != null) { existingSSOwner.OwnerId = owner.OwnerId; existingSSOwner.SubstationId = substation.SubstationId; await _context.SaveChangesAsync(); return(existingSSOwner); } } catch (DbUpdateException e) { _log.LogCritical($"Error occured while inserting SubstationOwner with webUatId {ssOwnerForeign.WebUatId}, owner id {owner.OwnerId} and ssId {substation.SubstationId}"); _log.LogCritical($"EntityWriteOption = {opt.ToString()}"); _log.LogCritical($"{e.Message}"); return(null); } return(null); }