diff --git a/DynamicLinqPadPostgreSqlDriver/Extensions/TableDataExtensions.cs b/DynamicLinqPadPostgreSqlDriver/Extensions/TableDataExtensions.cs index 02a1954..c616c9d 100644 --- a/DynamicLinqPadPostgreSqlDriver/Extensions/TableDataExtensions.cs +++ b/DynamicLinqPadPostgreSqlDriver/Extensions/TableDataExtensions.cs @@ -12,16 +12,21 @@ public static string CreateOneToManyAssociation(this TableData table, TableData // create an IEnumerable<> with the type of the (main) table's type builder var typedEnumerableType = typeof(IEnumerable<>).MakeGenericType(table.TypeBuilder); - // use the table's name as property name (plus constraint name) - var propertyName = $"{table.Name} ({constraintName})"; - foreignTable.PropertyAndFieldNames.Add(propertyName); + // use the table's name as property name + var propertyName = $"{table.ExplorerItem.Text}"; + var i = 1; + while (!foreignTable.PropertyAndFieldNames.Add(propertyName)) + { + propertyName = $"{table.ExplorerItem.Text}{i++}"; + } + // create a property in the foreign key's target table var property = foreignTable.TypeBuilder.DefineProperty(propertyName, typedEnumerableType); - + // create a getter for the property var propertyGetter = foreignTable.TypeBuilder.DefineGetter(property); - + // obtain ResolveOneToMany method var resolveOneToManyMethod = typeof(Entity).GetMethod("ResolveOneToMany", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(table.TypeBuilder); @@ -38,9 +43,14 @@ public static string CreateOneToManyAssociation(this TableData table, TableData // add the 'AssociationAttribute' to the property property.AddAssociationAttribute(foreignColumnNames, columnNames, table.Name); - + // create the explorer item - var explorerItem = new ExplorerItem(propertyName, ExplorerItemKind.CollectionLink, ExplorerIcon.OneToMany) { HyperlinkTarget = table.ExplorerItem }; + var explorerItem = new ExplorerItem(propertyName, ExplorerItemKind.CollectionLink, ExplorerIcon.OneToMany) + { + HyperlinkTarget = table.ExplorerItem, + ToolTipText = $"{table.ExplorerItem.Text} ({constraintName})" + }; + foreignTable.ExplorerItem.Children.Add(explorerItem); // create 'backward' association @@ -51,10 +61,15 @@ public static string CreateOneToManyAssociation(this TableData table, TableData public static string CreateManyToOneAssociation(this TableData table, TableData foreignTable, string columnNames, string foreignColumnNames, string constraintName, bool backwardReference = false) { - // use the foreign table's name as property name (plus constraint name) - var propertyName = $"{foreignTable.Name} ({constraintName})"; - table.PropertyAndFieldNames.Add(propertyName); - + // use the foreign table's name as property name (ToDo singularize?) + var propertyName = $"{foreignTable.ExplorerItem.Text}"; + + var i = 1; + while (!table.PropertyAndFieldNames.Add(propertyName)) + { + propertyName = $"{foreignTable.ExplorerItem.Text}{i++}"; + } + // create a property of the foreign table's type in the table entity var property = table.TypeBuilder.DefineProperty(propertyName, foreignTable.TypeBuilder); @@ -79,7 +94,12 @@ public static string CreateManyToOneAssociation(this TableData table, TableData property.AddAssociationAttribute(foreignColumnNames, columnNames, foreignTable.Name, backwardReference); // create the explorer item - var explorerItem = new ExplorerItem(propertyName, ExplorerItemKind.ReferenceLink, ExplorerIcon.ManyToOne) { HyperlinkTarget = foreignTable.ExplorerItem }; + var explorerItem = new ExplorerItem(propertyName, ExplorerItemKind.ReferenceLink, ExplorerIcon.ManyToOne) + { + HyperlinkTarget = foreignTable.ExplorerItem, + ToolTipText = $"{foreignTable.ExplorerItem.Text} ({constraintName})" + }; + table.ExplorerItem.Children.Add(explorerItem); return property.Name;