Exemplo n.º 1
0
        /// <summary>
        /// Wraps the collection in a DataReader, but also includes columns for the key attributes of related Entities.
        /// </summary>
        /// <typeparam name="T">The element type of the collection.</typeparam>
        /// <param name="collection">A collection to wrap in a DataReader</param>
        /// <param name="cx">The Entity Framework ObjectContext, used for metadata access</param>
        /// <returns>A DbDataReader wrapping the collection.</returns>
        public static IDataReader AsDataReader <T>(this IEnumerable <T> collection, ObjectContext context) where T : EntityObject
        {
            EntityDataReaderOptions options = EntityDataReaderOptions.Default;

            options.RecreateForeignKeysForEntityFrameworkEntities = true;
            return(new EntityDataReader <T>(collection, options, context));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Wraps the collection in a DataReader, but also includes columns for the key attributes of related Entities.
        /// </summary>
        /// <typeparam name="T">The element type of the collectin.</typeparam>
        /// <param name="collection">A collection to wrap in a DataReader</param>
        /// <param name="detachObjects">Option to detach each object in the collection from the ObjectContext.  This can reduce memory usage for queries returning large numbers of objects.</param>
        /// <param name="prefixReladObjectColumns">If True, qualify the related object keys, if False don't</param>
        /// <returns>A DbDataReader wrapping the collection.</returns>
        /// <summary>
        public static IDataReader AsDataReader <T>(this IEnumerable <T> collection, ObjectContext context, bool detachObjects, bool prefixRelatedObjectColumns) where T : EntityObject
        {
            EntityDataReaderOptions options = EntityDataReaderOptions.Default;

            options.RecreateForeignKeysForEntityFrameworkEntities = true;
            options.PrefixRelatedObjectColumns = prefixRelatedObjectColumns;

            if (detachObjects)
            {
                return(new EntityDataReader <T>(collection.DetachAllFrom(context), options, context));
            }
            return(new EntityDataReader <T>(collection, options, context));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Enumerates the collection and copies the data into a DataTable.
        /// </summary>
        /// <typeparam name="T">The element type of the collection.</typeparam>
        /// <param name="collection">The collection to copy to a DataTable</param>
        /// <returns>A DataTable containing the scalar projection of the collection.</returns>
        public static DataTable ToDataTable <T>(this IEnumerable <T> collection)
        {
            DataTable t = new DataTable();

            t.TableName = typeof(T).Name;
            EntityDataReaderOptions options = EntityDataReaderOptions.Default;

            options.ExposeNullableTypes = false;
            EntityDataReader <T> dr = new EntityDataReader <T>(collection, options);

            t.Load(dr);
            return(t);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Enumerates the collection and copies the data into a DataTable, but also includes columns for the key attributes of related Entities.
        /// </summary>
        /// <typeparam name="T">The element type of the collection.</typeparam>
        /// <param name="collection">The collection to copy to a DataTable</param>
        /// <param name="cx">The Entity Framework ObjectContext, used for metadata access</param>
        /// <returns>A DataTable containing the scalar projection of the collection.</returns>
        public static DataTable ToDataTable <T>(this IEnumerable <T> collection, ObjectContext cx) where T : EntityObject
        {
            DataTable t = new DataTable();

            t.TableName = typeof(T).Name;

            EntityDataReaderOptions options = EntityDataReaderOptions.Default;

            options.RecreateForeignKeysForEntityFrameworkEntities = true;

            EntityDataReader <T> dr = new EntityDataReader <T>(collection, options, cx);

            t.Load(dr);
            return(t);
        }
Exemplo n.º 5
0
        public EntityDataReader(IEnumerable <T> col, EntityDataReaderOptions options, ObjectContext objectContext)
        {
            this.enumerator = col.GetEnumerator();
            this.options    = options;

            if (options.RecreateForeignKeysForEntityFrameworkEntities && objectContext == null)
            {
                throw new ArgumentException("If RecreateForeignKeysForEntityFrameworkEntities=true then objectContext is required");
            }

            //done without a lock, so we risk running twice.  These collecitons are static so these collecitons
            //only need to be initialized once.
            if (scalarAttributes == null)
            {
                scalarAttributes = DiscoverScalarAttributes(typeof(T));
            }
            if (scalarAttributesPlusRelatedObjectScalarAttributes == null)
            {
                var atts = DiscoverRelatedObjectScalarAttributes(typeof(T), false);
                scalarAttributesPlusRelatedObjectScalarAttributes = atts.Concat(scalarAttributes).ToList();

                atts = DiscoverRelatedObjectScalarAttributes(typeof(T), true);
                scalarAttributesPlusComplexTypeAttributes = atts.Concat(scalarAttributes).ToList();
            }

            if (options.RecreateForeignKeysForEntityFrameworkEntities && scalarAttributesPlusRelatedObjectKeyAttributes == null)
            {
                var atts = DiscoverRelatedObjectKeyAttributes(typeof(T), objectContext);
                scalarAttributesPlusRelatedObjectKeyAttributes = atts.Concat(scalarAttributes).ToList();
            }

            if (options.FlattenRelatedObjects)
            {
                attributes = scalarAttributesPlusRelatedObjectScalarAttributes;
            }
            else if (objectContext != null)
            {
                attributes = scalarAttributesPlusRelatedObjectKeyAttributes;
            }
            else
            {
                attributes = scalarAttributesPlusComplexTypeAttributes;
            }
        }
Exemplo n.º 6
0
        public EntityDataReader(IEnumerable <T> col, EntityDataReaderOptions options, ObjectContext objectContext, params string[] ignoreAttributeNames)
        {
            this.enumerator = col.GetEnumerator();
            this.options    = options;

            if (options.RecreateForeignKeysForEntityFrameworkEntities && objectContext == null)
            {
                throw new ArgumentException("If RecreateForeignKeysForEntityFrameworkEntities=true then objectContext is required");
            }

            //done without a lock, so we risk running twice
            if (scalarAttributes == null)
            {
                scalarAttributes = DiscoverScalarAttributes(typeof(T), ignoreAttributeNames);
            }
            if (options.FlattenRelatedObjects && scalarAttributesPlusRelatedObjectScalarAttributes == null)
            {
                var atts = DiscoverRelatedObjectScalarAttributes(typeof(T));
                scalarAttributesPlusRelatedObjectScalarAttributes = atts.Concat(scalarAttributes).ToList();
            }
            if (options.RecreateForeignKeysForEntityFrameworkEntities && scalarAttributesPlusRelatedObjectKeyAttributes == null)
            {
                var atts = DiscoverRelatedObjectKeyAttributes(typeof(T), objectContext);
                scalarAttributesPlusRelatedObjectKeyAttributes = atts.Concat(scalarAttributes).ToList();
            }

            if (options.FlattenRelatedObjects)
            {
                attributes = scalarAttributesPlusRelatedObjectScalarAttributes;
            }
            else if (objectContext != null)
            {
                attributes = scalarAttributesPlusRelatedObjectKeyAttributes;
            }
            else
            {
                attributes = scalarAttributes;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Wraps the IEnumerable in a DbDataReader, having one column for each "scalar" property of the type T.
        /// The collection will be enumerated as the client calls IDataReader.Read().
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static IDataReader AsDataReader <T>(this IEnumerable <T> collection, bool exposeNullableColumns, bool flattenRelatedObjects)
        {
            EntityDataReaderOptions options = new EntityDataReaderOptions(exposeNullableColumns, flattenRelatedObjects, true, false);

            return(new EntityDataReader <T>(collection, options, null));
        }
Exemplo n.º 8
0
 public EntityDataReader(IEnumerable <T> col, EntityDataReaderOptions options)
     : this(col, options, null)
 {
 }
Exemplo n.º 9
0
        /// <summary>
        /// Wraps the IEnumerable in a DbDataReader, having one column for each "scalar" property of the type T.
        /// The collection will be enumerated as the client calls IDataReader.Read().
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static IDataReader AsDataReader <T>(this IEnumerable <T> collection, bool exposeNullableColumns, bool flattenRelatedObjects, params string[] ignoreAttributeNames)
        {
            EntityDataReaderOptions options = new EntityDataReaderOptions(exposeNullableColumns, flattenRelatedObjects, true, false);

            return(new EntityDataReader <T>(collection, options, null, ignoreAttributeNames));
        }
Exemplo n.º 10
0
 public EntityDataReader(IEnumerable <T> col, EntityDataReaderOptions options, params string[] ignoreAttributeNames)
     : this(col, options, null, ignoreAttributeNames)
 {
 }