public IHttpActionResult DeleteRequestByID(string idPreRequest) { try { using (BDRAEntities db = new BDRAEntities()) { int idPreRqt = int.Parse(idPreRequest); var rta = new ResponseViewModel(); var objPreRequest = db.PreRequest.Where(p => p.preReq_id == idPreRqt).FirstOrDefault(); objPreRequest.preReq_state = false; db.SaveChanges(); rta.response = true; rta.message = "Se ha eliminado correctamente la pre solicitud: " + objPreRequest.preReq_id; return(Ok(rta)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public static VehicleModelViewModel CreateVehicleModelInDB(VehicleModel pVehicleModel) { try { using (BDRAEntities db = new BDRAEntities()) { db.VehicleModel.Add(pVehicleModel); db.SaveChanges(); var vehiclemodelDB = db.VehicleModel.Where(vm => vm.vehMdl_name == pVehicleModel.vehMdl_name) .Select(vm => new VehicleModelViewModel { id = vm.vehMdl_id, name = vm.vehMdl_name, description = vm.vehMdl_description }) .FirstOrDefault(); return(vehiclemodelDB); } } catch (Exception ex) { return(null); } }
public IHttpActionResult validateJobTitle(JobTitleViewModel pJobTitle) { try { using (BDRAEntities db = new BDRAEntities()) { JobTitleViewModel jobTitle; jobTitle = db.JobTitlesClient.Where(j => j.jtcl_description.ToUpper() == pJobTitle.description.ToUpper()) .Select(jt => new JobTitleViewModel { id = jt.jtcl_id, description = jt.jtcl_description }) .FirstOrDefault(); if (jobTitle == null) { JobTitlesClient jt = new JobTitlesClient(); jt.jtcl_description = pJobTitle.description; jt.jtcl_state = true; db.JobTitlesClient.Add(jt); db.SaveChanges(); } var jtcl = db.JobTitlesClient.Where(j => j.jtcl_description.ToUpper() == pJobTitle.description.ToUpper()) .Select(jt => new JobTitleViewModel { id = jt.jtcl_id, description = jt.jtcl_description }) .FirstOrDefault(); return(Ok(jtcl)); } } catch (Exception e) { return(BadRequest(e.Message)); throw; } }
public IHttpActionResult updateRequest(RequestViewModel pRequest) { try { using (BDRAEntities db = new BDRAEntities()) { var oRequest = db.Request.Where(r => r.rqt_id == pRequest.id).FirstOrDefault(); oRequest.rqt_firstVisitDate = pRequest.initialDate; oRequest.rqt_lastVisitDate = pRequest.lastDate; oRequest.prb_id = pRequest.probability.id; oRequest.rqt_primaryState = pRequest.parentState.id; oRequest.rqt_secondState = pRequest.childState.id; if (pRequest.contact != null) { if (pRequest.contact.id != 0) { oRequest.cnt_id = pRequest.contact.id; } } oRequest.cnl_id = pRequest.canal.id; oRequest.rqt_observation = pRequest.observation; db.SaveChanges(); ResponseViewModel response = new ResponseViewModel(); response.response = true; response.message = "Se ha actualizado la solicitud: " + pRequest.id; return(Ok(response)); } } catch (Exception ex) { return(BadRequest(ex.Message)); throw; } }
public IHttpActionResult AddNewPreRequest(PreRequestViewModel pPreRequest) { try { this.objPreClientController = new PreClientController(); ResponseViewModel rta = new ResponseViewModel(); using (BDRAEntities db = new BDRAEntities()) { /* * Validamos si el PreCliente de la solicitud ya existe en la base de datos. */ PreClient PreClientBD = new PreClient(); PreClientBD = objPreClientController.GetPreClientBD(pPreRequest.preClient); if (PreClientBD == null) { PreClientBD = objPreClientController.SetDataToPreClient(pPreRequest.preClient, pPreRequest.user); var preClientCreated = objPreClientController.CreatePreClientInBD(PreClientBD); if (preClientCreated) { PreClientBD = objPreClientController.GetPreClientBD(pPreRequest.preClient); } } PreRequest oPreRequest = new PreRequest(); oPreRequest.preReq_registrationDate = DateTime.Now; oPreRequest.preCli_id = PreClientBD.preCli_id; oPreRequest.preReq_state = true; oPreRequest.usu_document = pPreRequest.user.id; if (pPreRequest.vehicleModel.id != null && pPreRequest.vehicleModel.id != 0) { oPreRequest.vehMdl_id = pPreRequest.vehicleModel.id; } else { var vechicleModel = VehicleModelController.SetDataToVehicleModel(pPreRequest.vehicleModel); var vehicleModelDB = VehicleModelController.CreateVehicleModelInDB(vechicleModel); oPreRequest.vehMdl_id = vehicleModelDB.id; } if (pPreRequest.stateRequest != null) { oPreRequest.sta_id = pPreRequest.stateRequest.id; } if (pPreRequest.firstCanal != null) { oPreRequest.firstCanal_id = pPreRequest.firstCanal.id; } if (pPreRequest.secondCanal != null) { oPreRequest.secondCanal_id = pPreRequest.secondCanal.id; } db.PreRequest.Add(oPreRequest); db.SaveChanges(); var lastRequest = GetTheLastPreRequestByPreClient(PreClientBD); if (pPreRequest.lsObservation[0].observation != "") { PreRequest_Observation observation = this.SetDataToObservation(lastRequest.preReq_id, pPreRequest.lsObservation[0].observation, pPreRequest.user.id); this.InsertObservationInDB(observation); } rta.response = true; rta.message = "Se ha creado la pre solicitud: " + lastRequest.preReq_id + " de cliente de persona natural"; return(Ok(rta)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IHttpActionResult addRequest(RequestViewModel pRequest) { try { ResponseViewModel response = new ResponseViewModel(); using (BDRAEntities db = new BDRAEntities()) { Request oRequest = new Request(); oRequest.rqt_registrationDate = DateTime.Now; oRequest.rqt_firstVisitDate = pRequest.initialDate; oRequest.rqt_lastVisitDate = pRequest.lastDate; oRequest.prb_id = pRequest.probability.id; oRequest.cli_document = pRequest.client.id; oRequest.usu_document = pRequest.user.id; oRequest.rqt_primaryState = pRequest.parentState.id; oRequest.rqt_secondState = pRequest.childState.id; oRequest.rqt_state = true; if (pRequest.contact != null) { if (pRequest.contact.id != 0) { oRequest.cnt_id = pRequest.contact.id; } } oRequest.cnl_id = pRequest.canal.id; oRequest.rqt_observation = pRequest.observation; db.Request.Add(oRequest); db.SaveChanges(); var lastRequest = db.Request.Where(r => r.cli_document == pRequest.client.id) .OrderByDescending(r => r.rqt_registrationDate) .Select(r => new RequestViewModel { id = r.rqt_id }) .FirstOrDefault(); riskInformationByRequest riskInformation = new riskInformationByRequest(); riskInformation.rqt_id = lastRequest.id; riskInformation.ribr_ammountApproved = 0; riskInformation.ribr_state = true; db.riskInformationByRequest.Add(riskInformation); db.SaveChanges(); operationalInformationByRequest operationalInformation = new operationalInformationByRequest(); operationalInformation.rqt_id = lastRequest.id; operationalInformation.oibr_deliveredAmmount = 0; operationalInformation.oibr_deliveredVehicles = 0; operationalInformation.oibr_state = true; db.operationalInformationByRequest.Add(operationalInformation); db.SaveChanges(); response.response = true; response.message = "Se crea la solicitud N° " + lastRequest.id; return(Ok(response)); } } catch (Exception e) { return(BadRequest(e.Message)); throw; } }
public IHttpActionResult updateClient(ClientViewModel pClient) { try { ResponseViewModel response = new ResponseViewModel(); using (BDRAEntities db = new BDRAEntities()) { var oClientBd = db.Client.Where(cl => cl.cli_document == pClient.id).FirstOrDefault(); oClientBd.kod_id = pClient.kindOfDocument.id; oClientBd.cli_name = pClient.name; oClientBd.cli_lastName = pClient.lastName; oClientBd.cli_cellPhone = pClient.cellPhone; oClientBd.cli_phone = pClient.phone; oClientBd.cli_email = pClient.email; oClientBd.ea_id = pClient.economicActivity.id; oClientBd.cty_id = pClient.city.id; db.SaveChanges(); var branch = db.branch.Where(b => b.cli_document == pClient.id && b.bra_isMain == true) .Select(b => new BranchViewModel { id = b.bra_id, name = b.bra_name }) .FirstOrDefault(); if (branch == null) { branch bdBranch = new branch(); if (pClient.kindOfDocument.description.ToUpper() == "NIT") { bdBranch.bra_name = "Principal " + pClient.name; } else { bdBranch.bra_name = "Principal " + pClient.name + " " + pClient.lastName; } bdBranch.bra_isMain = true; bdBranch.cli_document = pClient.id; bdBranch.bra_state = true; db.branch.Add(bdBranch); db.SaveChanges(); branch = db.branch.Where(b => b.cli_document == pClient.id && b.bra_isMain == true) .Select(b => new BranchViewModel { id = b.bra_id, name = b.bra_name }) .FirstOrDefault(); } if (pClient.lsContacts != null) { foreach (var contact in pClient.lsContacts) { if (contact.id == null || contact.id == 0) { Contact bdContact = new Contact(); bdContact.bra_id = branch.id; bdContact.cnt_name = contact.name; bdContact.cnt_lastName = contact.lastName; bdContact.cnt_cellPhone = contact.cellPhone; bdContact.cnt_phone = contact.phone; bdContact.cnt_email = contact.email; bdContact.cnt_adress = contact.adress; bdContact.jtcl_id = contact.jobTitle.id; db.Contact.Add(bdContact); db.SaveChanges(); } } } response.response = true; response.message = "Se ha actualizado el cliente: " + pClient.name + " " + pClient.lastName; return(Ok(response)); } } catch (Exception ex) { return(BadRequest(ex.Message)); throw; } }
public void CreateRequests(List <RequestViewModel> lsRequests, string separator, string headers, out List <string> lsDataErrorRqt) { lsDataErrorRqt = new List <string>(); int cont = 1; try { using (BDRAEntities db = new BDRAEntities()) { foreach (var rqt in lsRequests) { try { Console.WriteLine("Registro BD #: " + cont); Request oRqt = new Request(); oRqt.rqt_registrationDate = rqt.registrationDate.Value; oRqt.rqt_firstVisitDate = rqt.initialDate.Value; oRqt.rqt_lastVisitDate = rqt.lastDate.Value; oRqt.prb_id = rqt.probability.id; oRqt.rqt_primaryState = rqt.parentState.id; oRqt.rqt_secondState = rqt.childState.id; oRqt.cli_document = rqt.client.id; oRqt.usu_document = rqt.user.id; oRqt.cnt_id = rqt.contact.id; oRqt.rqt_state = true; db.Request.Add(oRqt); db.SaveChanges(); var lastRequest = db.Request.Where(r => r.cli_document == rqt.client.id) .OrderByDescending(r => r.rqt_registrationDate) .Select(r => new RequestViewModel { id = r.rqt_id }) .FirstOrDefault(); riskInformationByRequest riskInformation = new riskInformationByRequest(); riskInformation.rqt_id = lastRequest.id; riskInformation.ribr_ammountApproved = 0; riskInformation.ribr_state = true; db.riskInformationByRequest.Add(riskInformation); db.SaveChanges(); operationalInformationByRequest operationalInformation = new operationalInformationByRequest(); operationalInformation.rqt_id = lastRequest.id; operationalInformation.oibr_deliveredAmmount = 0; operationalInformation.oibr_deliveredVehicles = 0; operationalInformation.oibr_state = true; db.operationalInformationByRequest.Add(operationalInformation); db.SaveChanges(); cont++; } catch (Exception ex) { Console.WriteLine(ex.Message); cont++; continue; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public List <RequestViewModel> validateDataRequests(List <String> lsData, string separator) { String sline = "--------------------------------------------------------------------------"; var lsRequest = new List <RequestViewModel>(); int cont = 1; try { using (BDRAEntities db = new BDRAEntities()) { foreach (var data in lsData) { Console.WriteLine(sline); try { if (cont == 1) { cont += 1; continue; } Console.WriteLine("Registro #: " + cont); var aData = data.Split(separator); ClientViewModel oClient = new ClientViewModel(); string sClientDocument = aData[(int)atrRequest.NIT].ToString().Trim(); oClient = db.Client.Where(cl => cl.cli_document == sClientDocument) .Select(cl => new ClientViewModel { id = cl.cli_document, name = cl.cli_name }) .FirstOrDefault(); if (oClient == null) { throw new Exception("El cliente no existe en la base de datos."); } UserViewModel oManagerAccount = new UserViewModel(); string sNameAndLastName = aData[(int)atrRequest.User].ToString().Trim(); oManagerAccount = db.users.Where(us => (us.usu_name + " " + us.usu_lastName).Contains(sNameAndLastName)) .Select(us => new UserViewModel { id = us.usu_document }) .FirstOrDefault(); if (oManagerAccount == null) { throw new Exception("El gerente de cuenta no se encuentra en la base de datos"); } ContactViewModel oContactBank = new ContactViewModel(); string sNameAndLastNameContactBank = aData[(int)atrRequest.ContactBank].ToString().Trim(); oContactBank = db.Contact.Where(cnt => cnt.bra_id == 1 && (cnt.cnt_name + " " + cnt.cnt_lastName).Contains(sNameAndLastNameContactBank)) .Select(cnt => new ContactViewModel { id = cnt.cnt_id, name = cnt.cnt_name, lastName = cnt.cnt_lastName }) .FirstOrDefault(); if (oContactBank == null) { Contact cntBnk = new Contact(); cntBnk.bra_id = 1; var aNameLastName = divideNameAndLastName(sNameAndLastNameContactBank); cntBnk.cnt_name = aNameLastName[0]; cntBnk.cnt_lastName = aNameLastName[1]; cntBnk.jtcl_id = 1031; cntBnk.cnt_costCenter = ""; cntBnk.cnt_registrationDate = DateTime.Now; db.Contact.Add(cntBnk); db.SaveChanges(); oContactBank = db.Contact.Where(cnt => cnt.bra_id == 1 && (cnt.cnt_name + " " + cnt.cnt_lastName).Contains(sNameAndLastNameContactBank)) .Select(cnt => new ContactViewModel { id = cnt.cnt_id, name = cnt.cnt_name, lastName = cnt.cnt_lastName }) .FirstOrDefault(); } DateTime dFisrtVisit = DateTime.Parse(aData[(int)atrRequest.FirstVisit].ToString().Trim()); string sDateLastVisit = aData[(int)atrRequest.LastVisit].ToString().Trim(); DateTime dLastVisit = new DateTime(); if (sDateLastVisit != "") { dLastVisit = DateTime.Parse(sDateLastVisit); } string sFirstState = aData[(int)atrRequest.FirstState].ToString().Trim(); StateViewModel oFirstState = new StateViewModel(); oFirstState = db.states.Where(st => st.sta_description == sFirstState) .Select(st => new StateViewModel { id = st.sta_id, description = st.sta_description }) .FirstOrDefault(); if (oFirstState == null) { throw new Exception("No se encuentra el estado principal en la base de datos"); } string sTirdState = aData[(int)atrRequest.TirdState].ToString().Trim(); string sSecondState = ""; if (sTirdState != "") { sSecondState = sTirdState; } else { sSecondState = aData[(int)atrRequest.SecondState].ToString().Trim(); } StateViewModel oSecondState = new StateViewModel(); oSecondState = db.states.Where(st => st.sta_description.Contains(sSecondState)) .Select(st => new StateViewModel { id = st.sta_id, description = st.sta_description }) .FirstOrDefault(); if (oSecondState == null) { throw new Exception("No se encuentra el estado secundario en la base de datos"); } string sProbability = aData[(int)atrRequest.Probability].ToString().Trim(); ProbabilityViewModel oProbability = new ProbabilityViewModel(); oProbability = db.probability.Where(pb => pb.prb_description.ToUpper() == sProbability.ToUpper()) .Select(pb => new ProbabilityViewModel { id = pb.prb_id, description = pb.prb_description }) .FirstOrDefault(); if (oProbability == null) { throw new Exception("No se encuentra la probabilidad en la base de datos"); } string sRiskState = aData[(int)atrRequest.RiskState].ToString().Trim(); StateViewModel oRiskState = new StateViewModel(); if (sRiskState != "") { oRiskState = db.states.Where(st => st.stGrp_id == 2 && st.sta_description.ToUpper() == sRiskState) .Select(st => new StateViewModel { id = st.sta_id, description = st.sta_description }) .FirstOrDefault(); } string sAmmountApproved = aData[(int)atrRequest.ApprovedAmount].ToString().Trim(); string sRadicationDate = aData[(int)atrRequest.RiskRadication].ToString().Trim(); RiskInformationViewModel rkInf = new RiskInformationViewModel(); rkInf.riskState = oRiskState; if (sAmmountApproved == "") { sAmmountApproved = "0"; } rkInf.ammountApproved = long.Parse(sAmmountApproved); rkInf.dateSubmissionAnalysis = null; string sUserUpdateRisk = aData[(int)atrRequest.UserUpdateRisk].ToString().Trim(); DateTime dRegistrationDate = DateTime.Parse(aData[(int)atrRequest.CreationDate].ToString()); Console.WriteLine("Cliente: " + oClient.name); Console.WriteLine("Gerente de cuenta: " + oManagerAccount.id); Console.WriteLine("Gerente del banco: " + oContactBank.id); Console.WriteLine("Fecha primer visita: " + dFisrtVisit); Console.WriteLine("Fecha de la segunda visita: " + dLastVisit); Console.WriteLine("Estado principal: " + oFirstState.id); Console.WriteLine("Estado secundario: " + oSecondState.id); Console.WriteLine("Probabilidad: " + oProbability.id); Console.WriteLine("Fecha de registro: " + dRegistrationDate); Console.WriteLine("########################################"); Console.WriteLine("Información de Riesgos"); Console.WriteLine("########################################"); Console.WriteLine("Estado de riesgo: " + rkInf.riskState.description); Console.WriteLine("Fecha de radicación riesgo: " + rkInf.dateSubmissionAnalysis); Console.WriteLine("Monto Aprobado: " + rkInf.ammountApproved); Console.WriteLine("Usuario Actualización: "); Console.WriteLine("########################################"); Console.WriteLine("Información de Operaciones"); Console.WriteLine("########################################"); RequestViewModel rqt = new RequestViewModel(); rqt.client = oClient; rqt.user = oManagerAccount; rqt.contact = oContactBank; rqt.initialDate = dFisrtVisit; rqt.lastDate = dLastVisit; rqt.parentState = oFirstState; rqt.childState = oSecondState; rqt.probability = oProbability; rqt.registrationDate = dRegistrationDate; rqt.riskInformation = new RiskInformationViewModel(); rqt.riskInformation.riskState = oRiskState; lsRequest.Add(rqt); cont += 1; } catch (Exception ex) { Console.WriteLine(ex); cont += 1; continue; } } } } catch (Exception ex) { Console.WriteLine("Problemas de ejecución en la base de datos"); Console.WriteLine(ex.Message); } return(lsRequest); }
public void createContacts(List <ContactViewModel> lsContacts, string separator, string headers, out List <string> lsDataError) { String sline = "--------------------------------------------------------------------------"; int cont = 1; lsDataError = new List <string>(); lsDataError.Add(headers); Console.WriteLine("Cantidad de Contactos a agregar: " + lsContacts.Count); using (BDRAEntities db = new BDRAEntities()) { foreach (var oContact in lsContacts) { try { Console.WriteLine(sline); Console.WriteLine("Registro #: " + cont); Console.WriteLine("Branch: " + oContact.branch.id); Console.WriteLine("Nombres: " + oContact.name); Console.WriteLine("Apellidos: " + oContact.lastName); Console.WriteLine("Teléfono: " + oContact.phone); Console.WriteLine("Celular: " + oContact.cellPhone); Console.WriteLine("Dirección: " + oContact.adress); Console.WriteLine("Email: " + oContact.email); Console.WriteLine("Cargo: " + oContact.jobTitle.id); Console.WriteLine("Fecha de registro: " + DateTime.Now); Contact cnt = new Contact(); cnt.bra_id = oContact.branch.id; cnt.cnt_name = oContact.name; cnt.cnt_lastName = oContact.lastName; cnt.cnt_phone = oContact.phone; cnt.cnt_cellPhone = oContact.cellPhone; cnt.cnt_adress = oContact.adress; cnt.cnt_email = oContact.email; cnt.jtcl_id = oContact.jobTitle.id; cnt.cnt_registrationDate = DateTime.Now; cnt.cty_id = null; cnt.cnt_costCenter = null; db.Contact.Add(cnt); db.SaveChanges(); cont += 1; } catch (Exception ex) { var idClient = db.branch.Where(cl => cl.bra_id == oContact.branch.id).Select(cl => cl.cli_document).FirstOrDefault(); var dataError = idClient + separator + oContact.name.Trim() + " " + oContact.lastName.Trim() + separator + oContact.jobTitle.description.Trim() + separator + oContact.phone.Trim() + separator + oContact.cellPhone.Trim() + separator + oContact.adress.Trim() + separator + oContact.email.Trim() + "\n"; lsDataError.Add(dataError); Console.WriteLine("Error en registro: " + cont); Console.WriteLine(ex); cont += 1; db.Contact.Reverse(); continue; } } } }
public List <ContactViewModel> validateDataContact(List <string> lsData, string separator) { String sline = "--------------------------------------------------------------------------"; var lsContacts = new List <ContactViewModel>(); int cont = 0; ClientViewModel clientByContact = new ClientViewModel(); JobTitleViewModel jobTitle = new JobTitleViewModel(); BranchViewModel branchByContact = new BranchViewModel(); string sDocumentByClient = ""; string sNameByContact = ""; string sJobTitle = ""; string sPhone = ""; string sCellPhone = ""; string sAdress = ""; string sEmail = ""; try { using (BDRAEntities db = new BDRAEntities()) { foreach (var data in lsData) { cont += 1; if (cont == 1) { continue; } Console.WriteLine(sline); Console.WriteLine("Validanto linea: " + cont); Console.WriteLine(data); var aData = data.Split(separator); sDocumentByClient = aData[(int)atrContact.clientDocument].ToString(); clientByContact = db.Client.Where(cl => cl.cli_document == sDocumentByClient) .Select(cl => new ClientViewModel { id = cl.cli_document, name = cl.cli_name }) .FirstOrDefault(); if (clientByContact != null) { Console.WriteLine("Cliente: " + clientByContact.name); sNameByContact = aData[(int)atrContact.name].ToString(); Console.WriteLine("Contacto: " + sNameByContact); var name = ""; var lastName = ""; var aName = sNameByContact.Split(" "); for (int i = 0; i < aName.Length; i++) { if (aName.Length == 2) { if (i == 0) { name = aName[i]; } else { lastName = aName[i]; } } else { if (i <= 1) { name += aName[i] + " "; } else { lastName += aName[i] + " "; } } } Console.WriteLine("Nombre del contacto: " + name); if (name.Trim() == "") { Console.WriteLine("Contacto no valido ............."); continue; } Console.WriteLine("Apellido del contacto: " + lastName); sJobTitle = aData[(int)atrContact.jobTitle].ToString().ToUpper().Trim(); sJobTitle = sJobTitle.Replace(" ", " "); Console.WriteLine("Cargo: " + sJobTitle); jobTitle = db.JobTitlesClient.Where(jt => jt.jtcl_description.ToUpper() == sJobTitle) .Select(jt => new JobTitleViewModel { id = jt.jtcl_id, description = jt.jtcl_description }) .FirstOrDefault(); if (jobTitle == null) { JobTitlesClient jt = new JobTitlesClient(); jt.jtcl_description = sJobTitle; jt.jtcl_state = true; db.JobTitlesClient.Add(jt); db.SaveChanges(); Console.WriteLine("Se crea cargo en la bd..."); jobTitle = db.JobTitlesClient.Where(jt => jt.jtcl_description.ToUpper() == sJobTitle) .Select(jt => new JobTitleViewModel { id = jt.jtcl_id, description = jt.jtcl_description }) .FirstOrDefault(); } sPhone = aData[(int)atrContact.phone].ToString().Trim(); Console.WriteLine("Teléfono: " + sPhone); sCellPhone = aData[(int)atrContact.cellphone].ToString().Trim(); Console.WriteLine("Celular: " + sCellPhone); sAdress = aData[(int)atrContact.adress].ToString().Trim(); Console.WriteLine("Dirección: " + sAdress); sEmail = aData[(int)atrContact.email].ToString().Trim(); Console.WriteLine("Email: " + sEmail); branchByContact = db.branch.Where(b => b.cli_document == clientByContact.id && b.bra_isMain == true) .Select(b => new BranchViewModel { id = b.bra_id, name = b.bra_name }) .FirstOrDefault(); if (branchByContact == null) { branch branchCnt = new branch(); branchCnt.bra_isMain = true; branchCnt.bra_state = true; branchCnt.bra_name = "PRINCIPAL " + clientByContact.name; branchCnt.cli_document = clientByContact.id; db.branch.Add(branchCnt); db.SaveChanges(); branchByContact = db.branch.Where(b => b.cli_document == clientByContact.id && b.bra_isMain == true) .Select(b => new BranchViewModel { id = b.bra_id, name = b.bra_name }) .FirstOrDefault(); } Console.WriteLine("Sucursal id: " + branchByContact.id + " - " + branchByContact.name); ContactViewModel cnt = new ContactViewModel(); cnt.branch = branchByContact; cnt.name = name; cnt.lastName = lastName; cnt.phone = sPhone; cnt.cellPhone = sCellPhone; cnt.adress = sAdress; cnt.email = sEmail; cnt.jobTitle = jobTitle; lsContacts.Add(cnt); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(lsContacts); }