public bool Update(TEntity toUpdate) { bool Result = false; try { EntitySet.Attach(toUpdate); Context.Entry <TEntity>(toUpdate).State = EntityState.Modified; Result = Context.SaveChanges() > 0; } catch (DbEntityValidationException ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, EntityValidationErrors = ex.EntityValidationErrors }); this.EscribirEnArchivoLog(ex); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite }); this.EscribirEnArchivoLog(ex); } return(Result); }
public List <TEntity> Filter(Expression <Func <TEntity, bool> > criterio) { List <TEntity> Result = null; try { Result = EntitySet.Where(criterio).ToList(); } catch (DbEntityValidationException ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, EntityValidationErrors = ex.EntityValidationErrors }); this.EscribirEnArchivoLog(ex); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite }); this.EscribirEnArchivoLog(ex); } return(Result); }
public TEntity Create(TEntity toCreate) { TEntity Result = null; try { Result = EntitySet.Add(toCreate); Context.SaveChanges(); } catch (DbEntityValidationException ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, EntityValidationErrors = ex.EntityValidationErrors }); this.EscribirEnArchivoLog(ex); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite }); this.EscribirEnArchivoLog(ex); } return(Result); }
public bool Update(Expression <Func <TEntity, bool> > criterio, string propertyName, object valor) { bool Result = false; try { Context.Entry <TEntity>(EntitySet.FirstOrDefault(criterio)).Property(propertyName).CurrentValue = valor; Result = Context.SaveChanges() > 0; } catch (DbEntityValidationException ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, EntityValidationErrors = ex.EntityValidationErrors }); this.EscribirEnArchivoLog(ex); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite }); this.EscribirEnArchivoLog(ex); } return(Result); }
public TEntity Retrieve(Expression <Func <TEntity, bool> > criterio) { TEntity Result = null; try { Result = EntitySet.FirstOrDefault(criterio); } catch (DbEntityValidationException ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, EntityValidationErrors = ex.EntityValidationErrors }); this.EscribirEnArchivoLog(ex); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEventArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite }); this.EscribirEnArchivoLog(ex); } return(Result); }
public List <TEntity> Filter <TEntity>(Expression <Func <TEntity, bool> > criterio, string include1, string include2, string include3, string include4, string include5, string include6, string include7, string include8, string include9, string include10, bool asNoTrack = false) where TEntity : class { List <TEntity> Result = null; try { if (asNoTrack) { Result = Contexto.Set <TEntity>().AsNoTracking().Include(include1).Include(include2).Include(include3).Include(include4).Include(include5).Include(include6).Include(include7).Include(include10).Where(criterio).ToList(); } else { Result = Contexto.Set <TEntity>().Include(include1).Include(include2).Include(include3).Include(include4).Include(include5).Include(include6).Include(include7).Include(include8).Include(include9).Include(include10).Where(criterio).ToList(); } } catch (DbUpdateException ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink, Entries = ex.Entries }); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink }); } return(Result); }
public bool Delete <TEntity>(TEntity toDelete) where TEntity : class { bool Result = false; try { Contexto.Set <TEntity>().Attach(toDelete); Contexto.Set <TEntity>().Remove(toDelete); Result = Save() > 0; } catch (DbUpdateException ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink, Entries = ex.Entries }); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink }); } return(Result); }
public TEntity Create <TEntity>(TEntity toCreate) where TEntity : class { TEntity Result = null; try { Result = Contexto.Set <TEntity>().Add(toCreate).Entity; Save(); } catch (DbUpdateException ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink, Entries = ex.Entries }); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink }); } return(Result); }
public IEnumerable <TEntity> Create <TEntity>(IEnumerable <TEntity> toCreate) where TEntity : class { IEnumerable <TEntity> Result = null; try { Contexto.Set <TEntity>().AddRange(toCreate); Save(); Result = toCreate; } catch (DbUpdateException ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink, Entries = ex.Entries }); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink }); } return(Result); }
public bool Update <TEntity>(Expression <Func <TEntity, bool> > criterio, string propertyName, object valor) where TEntity : class { bool Result = false; try { Contexto.Entry <TEntity>(Contexto.Set <TEntity>().FirstOrDefault(criterio)).Property(propertyName).CurrentValue = valor; Result = Save() > 0; } catch (DbUpdateException ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink, Entries = ex.Entries }); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink }); } return(Result); }
/// <summary> /// Recibe Cédula y valida si es correcta /// </summary> /// <param name="ced"></param> /// <returns></returns> private static bool Valida_Cedula(this object valor, string ced) { string c = ced.Replace("-", ""); string Cedula = c.Substring(0, c.Length - 1); string Verificador = c.Substring(c.Length - 1, 1); decimal suma = 0; int mod, res; for (int i = 0; i < Cedula.Length; i++) { if ((i % 2) == 0) { mod = 1; } else { mod = 2; } if (int.TryParse(Cedula.Substring(i, 1), out int dig)) { res = dig * mod; } else { return(false); } if (res > 9) { res = Convert.ToInt32(res.ToString().Substring(0, 1)) + Convert.ToInt32(res.ToString().Substring(1, 1)); } suma += res; } decimal el_numero = (10 - (suma % 10)) % 10; if ((el_numero.ToString() == Verificador) && (Cedula.Substring(0, 3) != "000")) { return(true); } else { Excepcion?.Invoke(new object(), new ExceptionEvenArgs() { Message = $"El documento '{ced}' no es valido\n" + "el dígito verificador debió ser " + el_numero.ToString() }); return(false); } }
/// <summary> /// Retorna una lista del tipo a consultar. /// Referencia: http://json2csharp.com/ /// Convierte un objeto JSon a una clase de C# /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="obj"></param> /// <param name="enlace"></param> /// <returns></returns> public static TEntity ConsultaApi <TEntity>(this object obj, string enlace) { TEntity resultado = default; HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(enlace.Trim()); try { var response = myReq.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); resultado = JsonConvert.DeserializeObject <TEntity>(reader.ReadToEnd()); } } catch (Exception ex) { EventLog.WriteEntry("ConsultaApi", ex.Message, EventLogEntryType.Error); if (ex.InnerException != null) { EventLog.WriteEntry("ConsultaApi", ex.InnerException.Message, EventLogEntryType.Error); if (ex.InnerException.InnerException != null) { EventLog.WriteEntry("ConsultaApi", ex.InnerException.InnerException.Message, EventLogEntryType.Error); if (ex.InnerException.InnerException.InnerException != null) { EventLog.WriteEntry("ConsultaApi", ex.InnerException.InnerException.InnerException.Message, EventLogEntryType.Error); } } } Excepcion?.Invoke(new object(), new ExceptionEvenArgs() { InnerException = ex.InnerException, Message = ex.Message, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite }); return(resultado); } return(resultado); }
public TEntity Retrieve <TEntity>(Expression <Func <TEntity, bool> > criterio, string include1, string include2, string include3, string include4, string include5, string include6, string include7, string include8, string include9, string include10) where TEntity : class { TEntity Result = null; try { Result = Contexto.Set <TEntity>().Include(include1).Include(include2).Include(include3).Include(include4).Include(include5).Include(include6).Include(include7).Include(include8).Include(include9).Include(include10).FirstOrDefault(criterio); } catch (DbUpdateException ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink, Entries = ex.Entries }); } catch (Exception ex) { Excepcion?.Invoke(this, new ExceptionEvenArgs() { Message = ex.Message, InnerException = ex.InnerException, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite, HelpLink = ex.HelpLink }); } return(Result); }
/// <summary> /// Envia un correo con los parámetros enviados a continuación a un o más destinatarios con /// uno o varios archivos adjunto /// </summary> /// <param name="correosPara"> /// Listado de correos destino /// </param> /// <param name="correosParaCopia"> /// Listado de correos a los cuales les llegará una copia del mensaje /// </param> /// <param name="correosParaCopiaOculta"> /// Listado de correos a los cuales les llegará una copia oculta del mensaje /// </param> /// <param name="attachments"> /// Listado de archivos adjuntos (La ruta y su extensión) /// </param> /// <param name="correoDesde"> /// Correo desde cual se envia el mensaje /// </param> /// <param name="clave"> /// Contraseña del correo para enviar /// </param> /// <param name="titulo"> /// Título que aparecerá al recibir el mensaje /// </param> /// <param name="asunto"> /// Asunto del mensaje /// </param> /// <param name="mensaje"> /// El cuerpo del mensaje, este puede ser texto plano o formateado con etiquetas HTML /// </param> /// <param name="isHTML"> /// Indica si en contenido a enviar es con formato HTML /// </param> /// <param name="mailPriority"> /// Indica el nivel de prioridad del correo a enviar /// </param> /// <param name="host"> /// Aquí pondremos nuestro host, ejemplos: smtp.gmail.com, smtp.live.com, smtp.aslan.com.do... /// </param> /// <param name="puerto"> /// Puerto usado por el servidor de correo para enviar el mensaje, comunmente son: 25 o 587 /// </param> /// <param name="enableSsl"> /// Indica si el mensaje se enviará con SSL habilitado /// </param> /// <param name="useDefaultCredentials"> /// Las credenciales a usar al autenticarnos en el servidor de correo /// </param> /// <returns></returns> public static bool EnvioDeCorreo(this object obj, IEnumerable <MailAddress> correosPara, IEnumerable <MailAddress> correosParaCopia, IEnumerable <MailAddress> correosParaCopiaOculta, IEnumerable <Attachment> attachments, string correoDesde, string clave, string titulo, string asunto, string mensaje, bool isHTML = true, MailPriority mailPriority = MailPriority.Normal, string host = "smtp.gmail.com", int puerto = 587, bool enableSsl = true, bool useDefaultCredentials = false) { MailMessage email = new MailMessage() { From = new MailAddress(correoDesde, titulo), Subject = asunto, Body = mensaje, IsBodyHtml = isHTML, Priority = mailPriority }; SmtpClient smtp = new SmtpClient { Host = host, Port = puerto, EnableSsl = enableSsl, UseDefaultCredentials = useDefaultCredentials, Credentials = new NetworkCredential(correoDesde, clave) }; if (attachments != null) { foreach (var item in attachments) { email.Attachments.Add(item); } } if (correosPara != null) { foreach (var correo in correosPara) { email.To.Add(correo); } } if (correosParaCopia != null) { foreach (var correo in correosParaCopia) { email.CC.Add(correo); } } if (correosParaCopiaOculta != null) { foreach (var correo in correosParaCopiaOculta) { email.Bcc.Add(correo); } } try { smtp.Send(email); email.Dispose(); return(true); } catch (Exception ex) { EventLog.WriteEntry("EnvioDeCorreo", ex.Message, EventLogEntryType.Error); if (ex.InnerException != null) { EventLog.WriteEntry("EnvioDeCorreo", ex.InnerException.Message, EventLogEntryType.Error); if (ex.InnerException.InnerException != null) { EventLog.WriteEntry("EnvioDeCorreo", ex.InnerException.InnerException.Message, EventLogEntryType.Error); if (ex.InnerException.InnerException.InnerException != null) { EventLog.WriteEntry("EnvioDeCorreo", ex.InnerException.InnerException.InnerException.Message, EventLogEntryType.Error); } } } Excepcion?.Invoke(new object(), new ExceptionEvenArgs() { InnerException = ex.InnerException, Message = ex.Message, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite }); return(false); } }