/// <summary> /// Crea una Tabla sencilla a partir de la Definición de una Entidad del Framework. /// </summary> /// <typeparam name="T">Tipo de la Entidad.</typeparam> /// <returns>Tabla con un Esquema acorde a <c>T</c>.</returns> public static DataTable CreateTable <T>(bool absoluteColumnNames) { Type entityType = typeof(T); DataTable table = new DataTable(entityType.Name); PropertyDescriptorCollection entityPropertiesCollection = TypeDescriptor.GetProperties(entityType); foreach (PropertyDescriptor entityProperty in entityPropertiesCollection) { PropertyInfo primaryKeyType = EntitiesUtil.GetPrimaryKeyPropertyInfo(entityProperty.PropertyType); if (primaryKeyType != null) { if (absoluteColumnNames) { table.Columns.Add(entityProperty.Name + "." + primaryKeyType.Name, primaryKeyType.PropertyType); } else { table.Columns.Add(primaryKeyType.Name, primaryKeyType.PropertyType); } } else if (entityProperty.PropertyType.IsGenericType && entityProperty.PropertyType.GetGenericTypeDefinition() .Equals(typeof(Nullable <>))) { table.Columns.Add(entityProperty.Name , Nullable.GetUnderlyingType(entityProperty.PropertyType)); } else { table.Columns.Add(entityProperty.Name, entityProperty.PropertyType); } } return(table); }
/// <summary> /// Obtiene las Propiedades de la Entidad que son consideradas para formar la Llave /// Primaria. /// </summary> /// <param name="entityType">Tipo de la Entidad.</param> /// <returns>Propiedades para Llave Primaria.</returns> public static PropertyInfo[] GetPrimaryKeyProperties(Type entityType) { return(EntitiesUtil.GetPropertiesWithSpecificAttribute(entityType, typeof(PrimaryKeyAttribute))); }