public static List <T> ExecuteProcedure <T>(string sName, Dictionary <string, object> Parameters) { try { var SqlParams = new List <SqlParameter>(); string sParamsNames = ""; foreach (var Parameter in Parameters) { if (Parameter.Value == null) { sParamsNames += (" @" + Parameter.Key + " = NULL,"); } else { string sParamName, sSpecialType = ""; sParamName = Parameter.Key; if (Parameter.Key.Contains("/")) { sParamName = Parameter.Key.Split('/')[0]; sSpecialType = Parameter.Key.Split('/')[1]; } var oSqlParam = new SqlParameter("_" + sParamName, Parameter.Value); if (sSpecialType != "") { oSqlParam.TypeName = sSpecialType; } SqlParams.Add(oSqlParam); sParamsNames += (" @" + sParamName + " = @_" + sParamName + ","); // SqlParams.Add(new SqlParameter("_" + Parameter.Key, Parameter.Value)); } } sParamsNames = (sParamsNames == "" ? "" : sParamsNames.Izquierda(sParamsNames.Length - 1)); /* var Pars = new ObjectParameter[Parameters.Count]; * int iCount = 0; * foreach (var Parameter in Parameters) * { * Pars[iCount++] = new ObjectParameter(Parameter.Key, Parameter.Value); * } */ var context = Datos.GetDataContext(); context.CommandTimeout = 60000; var oRes = context.ExecuteStoreQuery <T>(sName + sParamsNames, SqlParams.ToArray <object>()).ToList(); Datos.ReleaseDataContext(ref context); return(oRes); /* using (var context = ModelHelper.CreateDataContext()) * { * return context.ExecuteStoreQuery<T>(sName + sParamsNames, SqlParams.ToArray<object>()).ToList(); * // return context.ExecuteFunction<T>(sName, Pars).ToList(); * } */ } catch (Exception ex) { return(null); } }
public static List <T> ExecuteQuery <T>(string sCommand, params object[] oParams) { var context = Datos.GetDataContext(); var oDatos = context.ExecuteStoreQuery <T>(sCommand, oParams).ToList(); Datos.ReleaseDataContext(ref context); return(oDatos); }
public static T GetEntity <T>(Expression <Func <T, bool> > expression) where T : class { var context = Datos.GetDataContext(); var oEntity = context.CreateObjectSet <T>().Where(expression).FirstOrDefault(); Datos.ReleaseDataContext(ref context); return(oEntity); /* using (var context = ModelHelper.CreateDataContext()) * { * return context.CreateObjectSet<T>().Where(expression).FirstOrDefault(); * } */ }
public static List <T> GetListOf <T>(Expression <Func <T, bool> > expression) where T : class { var context = Datos.GetDataContext(); var oList = context.CreateObjectSet <T>().Where(expression).ToList(); Datos.ReleaseDataContext(ref context); return(oList); /* using (var context = Datos.GetDataContext()) * { * return context.CreateObjectSet<T>().Where(expression).ToList(); * } */ }
public static bool Exists <T>(Expression <Func <T, bool> > oExpression) where T : class { var context = Datos.GetDataContext(); bool bExists = context.CreateObjectSet <T>().Any(oExpression); Datos.ReleaseDataContext(ref context); return(bExists); /* using (var context = ModelHelper.CreateDataContext()) * { * return context.CreateObjectSet<T>().Any(oExpression); * } */ }
public static void Delete <TEntity>(EntityObject entity) where TEntity : class { if (entity == null) { throw new ArgumentNullException("entity"); } using (var context = Datos.GetDataContext()) { context.Attach(entity); context.DeleteObject(entity); context.SaveChanges(); } }
public static List <T> GetListOf <T>() where T : class { var context = Datos.GetDataContext(); var oList = context.CreateObjectSet <T>().ToList(); Datos.ReleaseDataContext(ref context); return(oList); // return General.GetListOf<T>(q => q is object); /* using (var context = Datos.GetDataContext()) * { * return context.CreateObjectSet<T>().ToList(); * } */ }
public static void SaveOrUpdate <T>(T oObject) where T : class { var oEntity = (oObject as EntityObject); if (oEntity == null) { throw new ArgumentNullException("entity"); } var context = Datos.GetDataContext(); //using (var context = ModelHelper.CreateDataContext()) //{ if (oEntity.EntityState.Equals(EntityState.Detached)) { if (oEntity.EntityKey == null) { ObjectStateEntry stateEntry = null; context.ObjectStateManager.TryGetObjectStateEntry(oEntity, out stateEntry); var objectSet = context.CreateObjectSet <T>(); if (stateEntry == null || stateEntry.EntityKey.IsTemporary) { objectSet.AddObject(oObject); } } context.AddObject(oEntity.EntityKey.EntitySetName, oEntity); } if (oEntity.EntityState.Equals(EntityState.Modified)) { context.AttachUpdated(oEntity); } context.SaveChanges(); //} Datos.ReleaseDataContext(ref context); }