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); }
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); } }