public List <FilterBankOwnerForeign> ExtractFilterBankOwnersForeign(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 :id=1 and PARENT_ENTITY='FILTER_BANK' 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 <FilterBankOwnerForeign> fbOwnersForeign = new List <FilterBankOwnerForeign>(); while (reader.Read()) { FilterBankOwnerForeign fbOwnerForeign = new FilterBankOwnerForeign(); fbOwnerForeign.WebUatId = reader.GetInt32(0); fbOwnerForeign.FilterBankWebUatId = reader.GetInt32(1); fbOwnerForeign.OwnerWebUatId = reader.GetInt32(2); fbOwnersForeign.Add(fbOwnerForeign); } reader.Dispose(); return(fbOwnersForeign); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } } } }
public async Task <FilterBankOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, FilterBankOwnerForeign fbOwnerForeign, EntityWriteOption opt) { // check if entity already exists FilterBankOwner existingFbOwner = await _context.FilterBankOwners.SingleOrDefaultAsync(fbO => fbO.WebUatId == fbOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingFbOwner != null) { return(existingFbOwner); } // find the FilterBank via the FilterBank WebUatId int fbWebUatId = fbOwnerForeign.FilterBankWebUatId; FilterBank filterBank = await _context.FilterBanks.SingleOrDefaultAsync(ss => ss.WebUatId == fbWebUatId); // if FilterBank doesnot exist, skip the import. Ideally, there should not be such case if (filterBank == null) { _log.LogCritical($"Unable to find FilterBank with webUatId {fbWebUatId} while inserting FilterBankOwner with webUatId {fbOwnerForeign.WebUatId}"); return(null); } // find the Owner of the substation via the Owner WebUatId int ownerWebUatId = fbOwnerForeign.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 FilterBank with webUatId {ownerWebUatId} while inserting FilterBankOwner with webUatId {fbOwnerForeign.WebUatId}"); return(null); } try { // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingFbOwner != null) { _context.FilterBankOwners.Remove(existingFbOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingFbOwner == null || (opt == EntityWriteOption.Replace && existingFbOwner != null)) { FilterBankOwner newFbOwner = new FilterBankOwner(); newFbOwner.OwnerId = owner.OwnerId; newFbOwner.FilterBankId = filterBank.FilterBankId; newFbOwner.WebUatId = fbOwnerForeign.WebUatId; _context.FilterBankOwners.Add(newFbOwner); await _context.SaveChangesAsync(); return(newFbOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingFbOwner != null) { existingFbOwner.OwnerId = owner.OwnerId; existingFbOwner.FilterBankId = filterBank.FilterBankId; await _context.SaveChangesAsync(); return(existingFbOwner); } } catch (DbUpdateException e) { _log.LogCritical($"Error occured while inserting FilterBankOwner with webUatId {fbOwnerForeign.WebUatId}, owner id {owner.OwnerId} and ssId {filterBank.SubstationId}"); _log.LogCritical($"EntityWriteOption = {opt.ToString()}"); _log.LogCritical($"{e.Message}"); return(null); } return(null); }