public static bool ActualizaTodos <T>(Expression <Func <T, bool> > match, Action <T> action) where T : class, new() { var resp = false; try { using (var context = new DBEJEMPLO()) { var items = context.Set <T>().Where(match); foreach (var item in items) { action(item); } resp = context.SaveChanges() > 0; } } catch (Exception ex) { throw new Exception(string.Format("Error: {0}", ex.Message)); } return(resp); }
private void OpGuardar_Click(object sender, EventArgs e) { if (proceso == accion.agregar) { if (CapturaCorrecta() == false) { MessageBox.Show("Captura Incorrecta"); return; } var registro = new elliot__1(); registro.idPersona = Int32.Parse(this.txIdPersona.Text); registro.Nombre = this.txNombre.Text.ToUpper(); registro.Domicilio = this.txDomicilio.Text.ToUpper(); registro.Rfc = this.txRfc.Text.ToUpper(); registro.FechaNac = this.FechaNac.Value; registro.Activo = this.CheckActivo.Checked; var BASE = new DBEJEMPLO(); BASE.elliot__1.Add(registro); BASE.SaveChanges(); DataGridView1.Enabled = true; llenar_Grid(); } if (proceso == accion.modificar) { DataGridView1[0, poc].Value = txIdPersona.Text.ToUpper(); DataGridView1[1, poc].Value = txNombre.Text.ToUpper(); DataGridView1[2, poc].Value = txDomicilio.Text.ToUpper(); DataGridView1[4, poc].Value = txRfc.Text.ToUpper(); DataGridView1[3, poc].Value = FechaNac.Value; DataGridView1[5, poc].Value = CheckActivo.Checked; { MessageBox.Show("Se a modificado correctamente"); return; } } activar(); }
public static List <T> ConsultarListaDistinct <T>(Expression <Func <T, bool> > condicion) where T : class, new() { try { var context = new DBEJEMPLO(); var query = context.Set <T>().Where(condicion).Distinct(); return(query.ToList <T>()); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static List <T> ConsultarLista <T, TKey>(Expression <Func <T, TKey> > orden) where T : class, new() { try { var context = new DBEJEMPLO(); var query = context.Set <T>().OrderBy(orden); return(query.ToList <T>()); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static List <T> ConsultarLista <T>() where T : class, new() { try { var context = new DBEJEMPLO(); var query = context.Set <T>(); return(query.ToList <T>()); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
//nuevos public static bool ActualizaMultiple <T>(Expression <Func <T, bool> > condicion, Action <T> action) where T : class, new() { try { var context = new DBEJEMPLO(); var lista = context.Set <T>().Where(condicion).ToList <T>(); lista.ForEach(action); return(context.SaveChanges() > 0); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static List <T> ConsultarListaDesc <T, TKey>(Expression <Func <T, bool> > condicion, Expression <Func <T, TKey> > orden) where T : class, new() { try { using (var context = new DBEJEMPLO()) { var query = context.Set <T>().Where(condicion).OrderByDescending(orden); return(query.ToList <T>()); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static bool Agregar <T>(T objT) where T : class, new() { try { using (var context = new DBEJEMPLO()) { context.Set <T>().Add(objT); return(context.SaveChanges() > 0); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static List <TKey> ConsultarEscalar <T, TKey, TOrder>(Expression <Func <T, TKey> > filtro, Expression <Func <T, TOrder> > orden) where T : class, new() { try { using (var context = new DBEJEMPLO()) { var query = context.Set <T>().OrderBy(orden).Select(filtro); return(query.ToList()); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static List <object> ConsultarLista <T>(Expression <Func <T, object> > filtro) where T : class, new() { try { using (var context = new DBEJEMPLO()) { var query = context.Set <T>().Select(filtro); return(query.ToList()); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static List <TKey> ConsultarEscalarDisct <T, TKey>(Expression <Func <T, bool> > condicion, Expression <Func <T, TKey> > filtro) where T : class, new() { try { using (var context = new DBEJEMPLO()) { var query = context.Set <T>().Where(condicion).Select(filtro).Distinct(); return(query.ToList()); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static bool AddOrUpdate <T>(T obj, Func <T, bool> match) where T : class, new() { try { using (var context = new DBEJEMPLO()) { var lstObjT = (from objeto in context.Set <T>().Where <T>(match) select objeto).ToList(); if (lstObjT.Count > 0) { var dbEntityEntry = context.Entry <T>(obj); if (dbEntityEntry.State == EntityState.Detached) { var set = context.Set <T>(); T attachedEntity = set.Local.SingleOrDefault(match); if (attachedEntity != null) { var attachedEntry = context.Entry(attachedEntity); attachedEntry.CurrentValues.SetValues(obj); return(context.SaveChanges() > 0); } dbEntityEntry.State = EntityState.Modified; // This should attach entity return(context.SaveChanges() > 0); } dbEntityEntry.State = EntityState.Added; return(context.SaveChanges() > 0); } context.Set <T>().Add(obj); return(context.SaveChanges() > 0); } } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } return(false); }
public static List <TResult> GetDataTable <T, T1, TKey, TResult, TOrder>(Expression <Func <T, TKey> > join1, Expression <Func <T1, TKey> > join2, Expression <Func <T, T1, TResult> > exp, Expression <Func <T, bool> > match, Expression <Func <T, TOrder> > order) where T : class, new() where T1 : class, new() { try { using (var context = new DBEJEMPLO()) { var dbQuery = context.Set <T>().Where <T>(match).OrderBy(order).Join <T, T1, TKey, TResult>(context.Set <T1>(), join1, join2, exp); return((from obj in dbQuery orderby obj select obj).ToList()); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static bool Eliminar <T>(Expression <Func <T, bool> > condicion) where T : class, new() { try { using (var context = new DBEJEMPLO()) { var objDelete = context.Set <T>().Where(condicion).FirstOrDefault(); if (objDelete != null) { context.Set <T>().Remove(objDelete); } return(context.SaveChanges() > 0); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
public static bool EliminarTodos2 <T>(Expression <Func <T, bool> > condicion) where T : class { try { using (var context = new DBEJEMPLO()) { var query = context.Set <T>().Where(condicion); string selectSql = query.ToString(); string deleteSql = "DELETE [Extent1] " + selectSql.Substring(selectSql.IndexOf("FROM")); var internalQuery = query.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(field => field.Name == "_internalQuery").Select(field => field.GetValue(query)).First(); var objectQuery = internalQuery.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(field => field.Name == "_objectQuery").Select(field => field.GetValue(internalQuery)).First() as ObjectQuery; var parameters = objectQuery.Parameters.Select(p => new SqlParameter(p.Name, p.Value)).ToArray(); return(context.Database.ExecuteSqlCommand(deleteSql, parameters) > 0); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }