Esempio n. 1
0
        private void DataBaseTypeLink()
        {
            this.uxRelationName.Content = Integrity.BuildForeighKeyName(this.SelectedParentTable.ItemKey.ToString(), this.SelectedChildTable.ItemKey.ToString());

            foreach (ColumnObjectModel keyColumn in Integrity.GetObjectModel(this.SelectedParentTable.ItemKey.ToString()).Where(pk => pk.InPrimaryKey))
            {
                ComboBoxTool parentBox = new ComboBoxTool();

                ComboBoxTool childBox = new ComboBoxTool();

                parentBox.Name = $"{parentComboName}{this.uxParentColumns.Children.Count}";

                childBox.Name = $"{childComboName}{this.uxChildColumns.Children.Count}";

                parentBox.Items.Add(new DataItemModel {
                    DisplayValue = keyColumn.ColumnName, ItemKey = keyColumn.ColumnName
                });

                foreach (DataItemModel item in Integrity.GetColumnsForTable(this.SelectedChildTable.ItemKey.ToString()))
                {
                    childBox.Items.Add(item);
                }

                parentBox.SelectedValue = keyColumn.ColumnName;

                this.uxParentColumns.Children.Add(parentBox);

                this.uxChildColumns.Children.Add(childBox);
            }
        }
Esempio n. 2
0
        private void BuildRepositoryBase(string repositoryName, string dataContextName, string basePath, TableModel[] tables, IncludeTableModel[] includeTables)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine(EntityModelScript.Setup.RepositoryUsing);

            result.AppendLine();

            result.AppendLine($"namespace {EntityModelScript.Setup.RepositoryNamespace}");
            result.AppendLine("{");

            result.AppendLine($"    public abstract class {repositoryName}Repository_Base");
            result.AppendLine("    {");
            result.AppendLine($"        public {dataContextName} dataContext;");

            result.AppendLine();
            result.AppendLine($"        public {repositoryName}Repository_Base()");
            result.AppendLine("        {");
            result.AppendLine($"            this.dataContext = new {dataContextName}();");
            result.AppendLine("        }");

            foreach (TableModel table in tables)
            {
                string tableClassName = EntityModelScript.GetClassName(table);

                StringBuilder argumentsString = new StringBuilder();

                StringBuilder linqString = new StringBuilder();

                foreach (ColumnObjectModel keyColumn in table.Columns.Where(pk => pk.InPrimaryKey))
                {
                    string[] columnValues = EntityModelScript.GetColumnDotNetDescriptor(keyColumn);

                    argumentsString.Append($"{columnValues[0]} {columnValues[1]}, ");

                    linqString.Append($"pk.{columnValues[1]} == {columnValues[1]} && ");
                }

                if (linqString.Length > 4 && argumentsString.Length > 2)
                {
                    result.AppendLine();
                    result.AppendLine($"        public {tableClassName} Get{tableClassName}({(argumentsString.ParseToString().Substring(0, (argumentsString.Length - 2)))})");
                    result.AppendLine("        {");
                    result.AppendLine($"            return this.dataContext.{tableClassName}.FirstOrDefault(pk => {linqString.ParseToString().Substring(0, (linqString.Length - 4))});");
                    result.AppendLine("        }");
                }
            }

            foreach (IncludeTableModel table in includeTables)
            {
                string tableClassName = EntityModelScript.GetClassName(table);

                StringBuilder argumentsString = new StringBuilder();

                StringBuilder linqString = new StringBuilder();

                List <ColumnObjectModel> tableColumns = Integrity.GetObjectModel(table.TableName);

                if (tableColumns == null)
                {
                    continue;
                }

                foreach (ColumnObjectModel keyColumn in tableColumns.Where(pk => pk.InPrimaryKey))
                {
                    string[] columnValues = EntityModelScript.GetColumnDotNetDescriptor(keyColumn);

                    argumentsString.Append($"{columnValues[0]} {columnValues[1]}, ");

                    linqString.Append($"pk.{columnValues[1]} == {columnValues[1]} && ");
                }

                if (linqString.Length > 4 && argumentsString.Length > 2)
                {
                    result.AppendLine();
                    result.AppendLine($"        public {tableClassName} Get{tableClassName}({(argumentsString.ParseToString().Substring(0, (argumentsString.Length - 2)))})");
                    result.AppendLine("        {");
                    result.AppendLine($"            return this.dataContext.{tableClassName}.FirstOrDefault(pk => {linqString.ParseToString().Substring(0, (linqString.Length - 4))});");
                    result.AppendLine("        }");
                }
            }

            result.AppendLine("    }");
            result.AppendLine("}");

            File.WriteAllText(Path.Combine(basePath, $"{repositoryName}Repository_Base.cs"), result.ToString());
        }
Esempio n. 3
0
        private void Accept_Cliked(object sender, RoutedEventArgs e)
        {
            try
            {
                Dictionary <string, string> childToParentRelation = new Dictionary <string, string>();

                string parentTable = this.SelectedParentTable.ItemKey.ToString();

                string childTable = this.SelectedChildTable.ItemKey.ToString();

                UIElement[] parentBoxes = this.uxParentColumns.FindVisualControls(typeof(ComboBoxTool));

                foreach (ComboBoxTool childBox in this.uxChildColumns.Children)
                {
                    string parentBoxName = childBox.Name.Replace(childComboName, parentComboName);

                    ComboBoxTool parentBox = (ComboBoxTool)parentBoxes.First(b => ((ComboBoxTool)b).Name == parentBoxName);

                    if (childBox.SelectedItem == null || parentBox.SelectedItem == null)
                    {
                        continue;
                    }

                    string childColumnName = ((DataItemModel)childBox.SelectedItem).ItemKey.ToString();

                    string parentColumnName = ((DataItemModel)parentBox.SelectedItem).ItemKey.ToString();

                    if (childToParentRelation.ContainsKey(childColumnName))
                    {
                        throw new ApplicationException($"Cannot have duplicate selections for child {childBox.SelectedItem}.");
                    }

                    if (Integrity.GetGlobalColumnDataType(childColumnName) != Integrity.GetGlobalColumnDataType(parentColumnName))
                    {
                        throw new ApplicationException($"Inconsistent Data Types {parentColumnName} – {childColumnName}.");
                    }

                    childToParentRelation.Add(childColumnName, parentColumnName);
                }

                this.DatabaseRelation.ParentTable = parentTable;

                this.DatabaseRelation.ChildTable = childTable;

                this.DatabaseRelation.RelationshipName = this.uxRelationName.Content.ToString();

                this.DatabaseRelation.Columns.Clear();

                foreach (ColumnObjectModel column in Integrity.GetObjectModel(childTable))
                {
                    if (!childToParentRelation.ContainsKey(column.ColumnName))
                    {
                        continue;
                    }

                    column.ForeignConstraintName = this.DatabaseRelation.RelationshipName;

                    column.ForeignKeyColumn = childToParentRelation[column.ColumnName];

                    column.ForeignKeyTable = this.DatabaseRelation.ParentTable;

                    column.IsVertualRelation = (this.DatabaseRelation.RelationType == RelationTypesEnum.VirtualRelation);

                    column.IsForeignkey = true;

                    this.DatabaseRelation.Columns.Add(new ColumnRelationMapModel
                    {
                        ChildColumn           = column.ColumnName,
                        ChildTable            = this.DatabaseRelation.ChildTable,
                        ForeignConstraintName = this.DatabaseRelation.RelationshipName,
                        ParentColumn          = childToParentRelation[column.ColumnName],
                        ParentTable           = this.DatabaseRelation.ParentTable
                    });
                }

                this.DialogResult = true;

                //this.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.GetFullExceptionMessage());
            }
        }