public static void Importar(FixedIncomePosition jsonData) { List <DataEntities.MyInvests.InvestimentoRendaFixaPosicao> baseRendaFixa = LerDadosRendaFixa(jsonData); using (MyInvestsDataContext context = new MyInvestsDataContext()) { /////ATUALIZAR OS INVESTIMENTOS //foreach (var registroAdicionar in baseRendaFixa.Select(x => x.Investimento)) //{ // var registroDB = context.InvestimentoRendaFixa.Where(x => x.Id == registroAdicionar.Id).FirstOrDefault(); // if (registroDB == null) // { // registroDB = context.InvestimentoRendaFixa.Add(registroAdicionar); // context.SaveChanges(); // } //} //ATUALIZAR AS POSICOES foreach (var posicaoRF in baseRendaFixa) { var registroDB = context.InvestimentoRendaFixaPosicao .Where(x => x.IdInvestimento == posicaoRF.IdInvestimento && x.DataReferencia == posicaoRF.DataReferencia) .FirstOrDefault(); if (registroDB == null) { var investDB = context.InvestimentoRendaFixa.FirstOrDefault(x => x.Id == posicaoRF.IdInvestimento); if (investDB != null) { posicaoRF.Investimento = investDB; } registroDB = context.InvestimentoRendaFixaPosicao.Add(posicaoRF); context.SaveChanges(); } } } }
public void Importar(FundsPosition jsonDataFunds, List <Offer> jsonDataOffer) { ListaOfertasFundo = jsonDataOffer; PosicaoFundos = jsonDataFunds; List <Investimento> investimentosRico = ObterInformacaoInvestimentos(); using (MyInvestsDataContext context = new MyInvestsDataContext()) { //ATUALIZAR AS POSICOES foreach (var investimento in investimentosRico) { investimento.Fundo = ObterInformacoesFundo(investimento.IdFundo); var registroDB = context.FundosInvestimento .Where(x => x.IdFundo == investimento.IdFundo && x.DataCompra == investimento.DataCompra) .FirstOrDefault(); //SALVAR O INVESTIMENTO if (registroDB == null) { var fundoDB = context.Fundos.FirstOrDefault(x => x.Id == investimento.IdFundo); //se o fundo ja existir usar o que tem na base if (fundoDB != null) { investimento.Fundo = fundoDB; } registroDB = context.FundosInvestimento.Add(investimento); context.SaveChanges(); } else { registroDB.ValorIR = investimento.ValorIR; context.SaveChanges(); } //SALVAR A POSICAO DO FUNDO var posicaoFundo = ObterInformacoesPosicao(investimento.IdFundo); var posicaoDB = context.FundosPosicao .Where(x => x.IdFundo == posicaoFundo.IdFundo && x.DataReferencia == posicaoFundo.DataReferencia) .FirstOrDefault(); if (posicaoDB == null) { var fundoDB = context.Fundos.FirstOrDefault(x => x.Id == investimento.IdFundo); //se o fundo ja existir usar o que tem na base if (fundoDB != null) { posicaoFundo.Fundo = fundoDB; } context.FundosPosicao.Add(posicaoFundo); context.SaveChanges(); } } } }