using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Reflection; namespace MicroORM; public class OrmMetadata { public static string GetTableName() { var table = typeof(T).GetCustomAttribute(); if (table == null) throw new InvalidOperationException("La entidad no tiene [Table]"); return string.IsNullOrEmpty(table.Schema) ? table.Name : $"{table.Schema}.{table.Name}"; } public static PropertyInfo GetKeyProperty() { var key = typeof(T) .GetProperties() .FirstOrDefault(p => p.GetCustomAttribute() != null); if (key == null) throw new InvalidOperationException("La entidad no tiene [Key]"); return key; } public static string GetColumnName(PropertyInfo prop) { var columnAttr = prop.GetCustomAttribute(); return columnAttr?.Name ?? prop.Name; } public static bool IsIdentity(PropertyInfo prop) { var key = prop.GetCustomAttribute() != null; var dbGenerated = prop.GetCustomAttribute(); return key && dbGenerated?.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity; } public static IEnumerable GetScaffoldProperties() { var type = typeof(T); var key = GetKeyProperty(); return type.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.CanWrite && // tiene setter p.GetCustomAttribute() == null && ( p.GetCustomAttribute() != null || p == key ) ); } }