예제 #1
0
        public static List <T> Query <T>(this EFDBContext dbContext, CommandType eCommandType, string sql, List <SqlParameter> paramList) where T : DBEntity_Basic, new()
        {
            var result = new List <T>();

            var type = typeof(T);

            using (var connection = dbContext.Database.GetDbConnection())
                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandType = eCommandType;
                    cmd.CommandText = sql;

                    if (paramList != null && paramList.Count() != 0)
                    {
                        paramList.ForEach(o =>
                        {
                            if (o.IsNullable && o.Value == null)
                            {
                                o.Value = DBNull.Value;
                            }
                        });

                        cmd.Parameters.AddRange(paramList.ToArray());
                    }

                    connection.Open();

                    try
                    {
                        using (var reader = cmd.ExecuteReader())
                        {
                            var columnSchema = reader.GetColumnSchema();

                            while (reader.Read())
                            {
                                T item = new T();

                                foreach (var pi in type.GetProperties())
                                {
                                    object value = null;

                                    if (reader.IsColumnExist(pi.Name))
                                    {
                                        object obj_regular = reader[pi.Name];
                                        object obj_finnal  = obj_regular == DBNull.Value ? null : obj_regular.TypeTo(pi.PropertyType);
                                        value = obj_finnal;
                                    }

                                    pi.SetValue(item, value);
                                }

                                result.Add(item);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        connection.Close();
                    }
                }

            return(result);
        }
예제 #2
0
 public static IQueryable <T> Query <T>(this EFDBContext dbContext) where T : DBEntity_Basic, new()
 {
     return(dbContext.GetDBSet <T>().AsQueryable <T>());
 }