Пример #1
0
        private static DataTable DataTableFromType(Type type, SqlIntegrator integrator, bool excludeIdentity, out PropertyInfo[] properties)
        {
            DataTable result = new DataTable(integrator.GetTableName(type));

            properties = integrator.GetMappedColumns(type).ToArray();
            if (excludeIdentity)
            {
                var identityProp = type.GetIdentityProperty();
                if (identityProp != null)
                {
                    properties = properties.Except(new PropertyInfo[] { identityProp }).ToArray();
                }
            }

            List <DataColumn> pkColumns = new List <DataColumn>();

            foreach (PropertyInfo pi in properties)
            {
                DataColumn col = result.Columns.Add(pi.GetColumnName(), pi.GetMappedType());
                if (pi.HasAttribute <PrimaryKeyAttribute>())
                {
                    pkColumns.Add(col);
                }
            }

            result.PrimaryKey = pkColumns.ToArray();

            return(result);
        }
Пример #2
0
 private static IEnumerable <TypeExcludeRule> GetTypeExcludeRules(SqlIntegrator integrator)
 {
     return(new TypeExcludeRule[]
     {
         new TypeExcludeRule()
         {
             Rule = (t) => { return t.HasAttribute <NotMappedAttribute>(); },
             Description = "Has the [NotMapped] attribute"
         },
         new TypeExcludeRule()
         {
             Rule = (t) => { return t.IsAbstract; },
             Description = "Abstract classes not supported"
         },
         new TypeExcludeRule()
         {
             Rule = (t) => { return t.IsEnum; },
             Description = "Enums not supported"
         },
         new TypeExcludeRule()
         {
             Rule = (t) => { return !HasPrimaryKeyOrIdentity(integrator, t); },
             Description = "No [PrimaryKey] properties, Id property, nor [Identity] attribute found"
         }
     });
 }
Пример #3
0
        public static DataTable ToDataTable <T>(this IEnumerable <T> enumerable, SqlIntegrator integrator, bool excludeIdentity = false) where T : class
        {
            DataTable result = DataTableFromType(typeof(T), integrator, excludeIdentity, properties: out PropertyInfo[] properties);

            foreach (T item in enumerable)
            {
                result.Rows.Add(ValuesFromItem(properties, result, item));
            }
            return(result);
        }
Пример #4
0
        private static bool HasPrimaryKeyOrIdentity(SqlIntegrator integrator, Type t)
        {
            try
            {
                var pi = t.GetIdentityProperty();
                if (pi != null)
                {
                    return(true);
                }

                var pkCol = integrator.GetMappedColumns(t).Where(prop => prop.HasAttribute <PrimaryKeyAttribute>());
                return(pkCol.Any());
            }
            catch
            {
                return(false);
            }
        }