public StructuralType() { dataProperties = new List<DataProperty>(); custom = new CustomEntityMetaData(); }
public string GetCustomMetaData(DbContext dbContext) { var customMetaData = new CustomMetaData(); Type type = dbContext.GetType(); // Get all DbSets from DbContext. List<PropertyInfo> propertyInfos = type.GetProperties().Where(p => p.PropertyType.Name.Equals("DbSet`1")).ToList(); // Proces DbSets. foreach (PropertyInfo propertyInfo in propertyInfos) { var structuralType = new StructuralType(); // Proces Entity. Type entityType = propertyInfo.PropertyType.GenericTypeArguments[0]; structuralType.@namespace = entityType.Namespace; structuralType.shortName = entityType.Name; // Get all entity validation attributes (Attribute must implement IEntityValidator). IEnumerable<Attribute> entityAttributes = entityType.GetCustomAttributes().Where(x => x is IEntityValidator); // Proces entity validation attributes. foreach (Attribute entityAttribute in entityAttributes) { // Add entity validation attributes to custom metadata. var metaData = new CustomEntityMetaData(); structuralType.custom.validators.Add(entityAttribute as IEntityValidator); } // Get all fields (SQL Server columns) per Entity (SQL Server Table). PropertyInfo[] epis = entityType.GetProperties(); // Proces entity properties. foreach (PropertyInfo epi in epis) { var dataProperty = new DataProperty { nameOnServer = epi.Name }; if (epi.Name.Equals("Id", StringComparison.InvariantCultureIgnoreCase)) { dataProperty.isPartOfKey = true; } // Get all field validation attributes. IEnumerable<Attribute> epiAttributes = epi.GetCustomAttributes(); // Proces field validation attributes. foreach (Attribute epiAttribute in epiAttributes) { dataProperty.custom.validators.Add(epiAttribute); } structuralType.dataProperties.Add(dataProperty); } customMetaData.structuralTypes.Add(structuralType); } return JsonConvert.SerializeObject(customMetaData); }