public void AddTablesDescriptions(DbContext mydbContext, bool bBaseVide) { // Fetch all the DbContext class public properties which contains your attributes var dbContextProperies = typeof(DbContext).GetProperties().Select(pi => pi.Name).ToList(); // Loop each DbSets of type T foreach (var item in typeof(Context).GetProperties() .Where(p => dbContextProperies.IndexOf(p.Name) < 0) .Select(p => p)) { string sTable = item.Name; if (sTable.EndsWith("s")) { sTable = sTable.Substring(0, sTable.Length - 1); } string sDescr = ""; Type typeTable = Type.GetType("DicoLogotronMdb." + sTable); sDescr = typeTable.GetAttributeValue( (TableDescription descr) => descr.Value); //Debug.WriteLine("Table : " + sTable + " : " + sDescr); string sBase = clsConstMdb.sBaseLogotron; if (bBaseVide) { sBase = clsConstMdb.sBaseLogotronVide; } //string sBase = clsConstMdb.sBaseLogotronVide; string sChemin0 = Application.StartupPath + "\\" + sBase + clsConstMdb.sLang; string sCheminMdb = sChemin0 + clsConstMdb.sExtMdb; UtilDebuggerStepThrough.DefinirDescrTableOuColonne(sCheminMdb, sTable, "", sDescr); } }
public void AddColumnsDescriptions(DbContext mydbContext, bool bBaseVide) { // Fetch all the DbContext class public properties which contains your attributes var dbContextProperies = typeof(DbContext).GetProperties().Select(pi => pi.Name).ToList(); // Loop each DbSets of type T foreach (var item in typeof(Context).GetProperties() .Where(p => dbContextProperies.IndexOf(p.Name) < 0) .Select(p => p)) { if (!item.PropertyType.GetGenericArguments().Any()) { continue; } // Fetch the type of "T" var entityModelType = item.PropertyType.GetGenericArguments()[0]; var descriptionInfos = from prop in entityModelType.GetProperties() where prop.GetCustomAttributes(typeof(DescriptionAttribute), true).Any() select new { ColumnName = prop.Name, Attributes = prop.CustomAttributes }; foreach (var descriptionInfo in descriptionInfos) { // Sql to create the description column and adding // Cette requête ne fonctionne pas pour MS-Access : //var addDiscriptionColumnSql = // @"sp_addextendedproperty @name = N'MS_Description', @value = '" // + descriptionInfo.Attributes.First().ConstructorArguments.First() // + @"', @level0type = N'Schema', @level0name = dbo, @level1type = N'Table', @level1name = " // + entityModelType.Name + "s" + ", @level2type = N'Column', @level2name =" // + descriptionInfo.ColumnName; //var sqlCommandResult = mydbContext.Database.ExecuteSqlCommand(addDiscriptionColumnSql); //var arg = descriptionInfo.Attributes.First().ConstructorArguments.First(); string sDescr = ""; string sNomColonneModifie = ""; foreach (var attr in descriptionInfo.Attributes) { //Debug.WriteLine(attr.AttributeType.Name + "=" + attr.ToString()); if (attr.AttributeType.Name == "DescriptionAttribute") { var descr = attr.ConstructorArguments.First(); sDescr = descr.Value.ToString(); //break; } if (attr.AttributeType.Name == "ColumnAttribute") { var descr = attr.ConstructorArguments.First(); sNomColonneModifie = descr.Value.ToString(); //break; } } //string sDescr = arg.ToString(); //Debug.WriteLine("ColumnName : " + descriptionInfo.ColumnName + " : " + sDescr); string sBase = clsConstMdb.sBaseLogotron; if (bBaseVide) { sBase = clsConstMdb.sBaseLogotronVide; } //string sBase = clsConstMdb.sBaseLogotronVide; string sChemin0 = Application.StartupPath + "\\" + sBase + clsConstMdb.sLang; string sCheminMdb = sChemin0 + clsConstMdb.sExtMdb; string sCol = descriptionInfo.ColumnName; if (sNomColonneModifie.Length > 0) { sCol = sNomColonneModifie; } string sTable = item.Name; if (sTable.EndsWith("s")) { sTable = sTable.Substring(0, sTable.Length - 1); } UtilDebuggerStepThrough.DefinirDescrTableOuColonne(sCheminMdb, sTable, sCol, sDescr); } } }