public List <AcTransmissionLineCircuitOwnerForeign> ExtractAcTransLineCktOwnersForeign(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='AC_TRANSMISSION_LINE' 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 <AcTransmissionLineCircuitOwnerForeign> cktOwnersForeign = new List <AcTransmissionLineCircuitOwnerForeign>(); while (reader.Read()) { AcTransmissionLineCircuitOwnerForeign cktOwnerForeign = new AcTransmissionLineCircuitOwnerForeign(); cktOwnerForeign.WebUatId = reader.GetInt32(0); cktOwnerForeign.AcTranLineCktWebUatId = reader.GetInt32(1); cktOwnerForeign.OwnerWebUatId = reader.GetInt32(2); cktOwnersForeign.Add(cktOwnerForeign); } reader.Dispose(); return(cktOwnersForeign); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } } } }
public async Task <AcTransLineCktOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, AcTransmissionLineCircuitOwnerForeign acCktOwnerForeign, EntityWriteOption opt) { // check if entity already exists AcTransLineCktOwner existingAcCktOwner = await _context.AcTransLineCktOwners.SingleOrDefaultAsync(aco => aco.WebUatId == acCktOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingAcCktOwner != null) { return(existingAcCktOwner); } // find the AcTranLineCkt via the AcTranLineCkt WebUatId int acCktWebUatId = acCktOwnerForeign.AcTranLineCktWebUatId; AcTransLineCkt acTransCkt = await _context.AcTransLineCkts.SingleOrDefaultAsync(ss => ss.WebUatId == acCktWebUatId); // if AcTransLineCkt doesnot exist, skip the import. Ideally, there should not be such case if (acTransCkt == null) { _log.LogCritical($"Could not find AcTransLineCkt with WebUatId {acCktWebUatId} in warehouse while creating AcTransLineCktOwner with WebUat Id {acCktOwnerForeign.WebUatId}"); return(null); } // find the Owner of the AcTransLineCkt via the Owner WebUatId int ownerWebUatId = acCktOwnerForeign.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($"Could not find Owner with WebUatId {ownerWebUatId} in warehouse while creating AcTransLineCktOwner with WebUat Id {acCktOwnerForeign.WebUatId}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingAcCktOwner != null) { _context.AcTransLineCktOwners.Remove(existingAcCktOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingAcCktOwner == null || (opt == EntityWriteOption.Replace && existingAcCktOwner != null)) { AcTransLineCktOwner acCktOwner = new AcTransLineCktOwner(); acCktOwner.OwnerId = owner.OwnerId; acCktOwner.AcTransLineCktId = acTransCkt.AcTransLineCktId; acCktOwner.WebUatId = acCktOwnerForeign.WebUatId; _context.AcTransLineCktOwners.Add(acCktOwner); await _context.SaveChangesAsync(); return(acCktOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingAcCktOwner != null) { existingAcCktOwner.OwnerId = owner.OwnerId; existingAcCktOwner.AcTransLineCktId = acTransCkt.AcTransLineCktId; await _context.SaveChangesAsync(); return(existingAcCktOwner); } return(null); }