Exemple #1
0
        private void BuildModelFiles(ErdCanvasModel[] canvases, Dispatcher dispatcher)
        {
            string basePath = Path.Combine(EntityModelScript.Setup.ClientModelOutputDirectory, "Base");

            string userPath = Path.Combine(EntityModelScript.Setup.ClientModelOutputDirectory, "User");

            Paths.CreateDirectory(basePath);

            Paths.CreateDirectory(userPath);

            foreach (ErdCanvasModel canvas in canvases)
            {
                foreach (TableModel table in canvas.SegmentTables)
                {
                    dispatcher.Invoke(() => { EventParser.ParseMessage(this, new ParseMessageEventArguments {
                            Message = string.Format("Building Model {0}", table.TableName)
                        }); });

                    string modelBase = EntityModelScript.ScriptServerModelBase(table);

                    string className = EntityModelScript.GetClassName(table);

                    File.WriteAllText(Path.Combine(basePath, $"{className}_Base.cs"), modelBase);

                    string userFileName = Path.Combine(userPath, $"{className}.cs");

                    if (!File.Exists(userFileName))
                    {
                        string model = EntityModelScript.ScriptServerModel(table);

                        File.WriteAllText(userFileName, model);
                    }
                }
            }
        }
        //private static string GetModelFieldString(ColumnObjectModel column)
        //{

        //}

        private static string GetModelPropertyString(ColumnObjectModel column)
        {
            string sqlDataType = EntityModelScript.IsNullable(column) ?
                                 $"{EntityModelScript.GetSqlDataMap(column.SqlDataType.Value)}{(column.InPrimaryKey ? string.Empty : "?")}" :
                                 EntityModelScript.GetSqlDataMap(column.SqlDataType.Value);

            string inPrimaryKey = column.InPrimaryKey ? "(Primary Key) " : string.Empty;

            string isForeignKey = column.IsForeignkey ? $"(Foreign Key from: {column.ForeignKeyTable}) " : string.Empty;

            string description = $"{inPrimaryKey}{isForeignKey}{column.Description}";

            if (column.SqlDataType.Value == SqlDbType.Timestamp)
            {
                string resultSting = EntityModelScript.Setup.ModelPropertyString
                                     .Replace("{0}", description)
                                     .Replace("{1}", sqlDataType)
                                     .Replace("{2}", EntityModelScript.GetColumnName(column));

                int insertIndex = resultSting.IndexOf("</summary>") + 10;

                resultSting = resultSting.Insert(insertIndex, $"{Environment.NewLine}        [Timestamp]");

                return(resultSting.ToString());
            }

            // NOTE: We need to do a replace due to the brackets in the text here
            return(EntityModelScript.Setup.ModelPropertyString
                   .Replace("{0}", description)
                   .Replace("{1}", sqlDataType)
                   .Replace("{2}", EntityModelScript.GetColumnName(column)));
        }
Exemple #3
0
        private void RunMappingScriptor(ErdCanvasModel[] canvases, Dispatcher dispatcher)
        {
            try
            {
                string dispatchNote = "Build Mappings Script for: {0}";

                Paths.CreateDirectory(Path.Combine(EntityModelScript.Setup.ServerOutputBaseDirectory, "Mappings"));

                foreach (ErdCanvasModel canvas in canvases)
                {
                    foreach (TableModel table in canvas.SegmentTables)
                    {
                        dispatcher.Invoke(() => { EventParser.ParseMessage(this, new ParseMessageEventArguments {
                                Message = string.Format(dispatchNote, table.TableName)
                            }); });

                        string mapptingPath = Path.Combine(EntityModelScript.Setup.ServerOutputBaseDirectory, "Mappings", $"{EntityModelScript.GetClassName(table)}Mapping.cs");

                        string mappingResult = EntityModelScript.ScriptServerModelMapping(table);

                        File.WriteAllText(mapptingPath, mappingResult);
                    }
                }
            }
            catch (Exception err)
            {
                throw;
            }
        }
 public static string[] GetColumnDotNetDescriptor(ColumnObjectModel column)
 {
     return(new string[]
     {
         EntityModelScript.GetSqlDataMap(column.SqlDataType.Value),
         EntityModelScript.GetColumnName(column)
     });
 }
        public static string ScriptServerModel(TableModel table)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine("using System.ComponentModel.DataAnnotations.Schema;");
            result.AppendLine();

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

            result.AppendLine("{");

            result.AppendLine($"    {EntityModelScript.GetModelClassString(table)}");
            result.AppendLine("    {");

            result.AppendLine("        // NOTE: Add the [NotMapped] Attribute to all properties in this class to ensure that Entity Framework does not fall over");
            result.AppendLine("    }");

            result.Append("}");

            return(result.ToString());
        }
        public static string ScriptServerModelBase(TableModel table)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine("using System.ComponentModel.DataAnnotations;");

            result.AppendLine(Setup.ModelClassBaseUsing);

            result.AppendLine();

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

            result.AppendLine("{");

            result.AppendLine($"    {EntityModelScript.GetModelClassBaseString(table)}");
            result.AppendLine("    {");

            //private {1} _{2};

            foreach (ColumnObjectModel column in table.Columns)
            {
            }

            foreach (ColumnObjectModel column in table.Columns)
            {
                result.AppendLine($"{EntityModelScript.GetModelPropertyString(column)}");

                result.AppendLine();
            }

            result.AppendLine("    }");

            result.Append("}");

            return(result.ToString());
        }
Exemple #7
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());
        }
Exemple #8
0
        private void BuildContextFile(string fileName, TableModel[] tablesUsed, IncludeTableModel[] includeTables)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine(EntityModelScript.Setup.DataContextUsing);

            result.AppendLine();

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

            result.AppendLine("{");

            result.AppendLine($"    public class {fileName} : DbContext");
            result.AppendLine("    {");

            string getset = "{ get; set; }";

            List <string> tablesUsedList = new List <string>();

            foreach (TableModel table in tablesUsed)
            {
                string nameToUse = EntityModelScript.Setup.UseFriendlyNames ? table.FriendlyName.MakeAlphaNumeric() : table.TableName;

                result.AppendLine($"        public DbSet<{EntityModelScript.GetClassName(table)}> {nameToUse} {getset}");

                tablesUsedList.Add(table.TableName);
            }

            foreach (IncludeTableModel included in includeTables)
            {
                if (tablesUsedList.Contains(included.TableName))
                {
                    continue;
                }

                string nameToUse = EntityModelScript.Setup.UseFriendlyNames ? included.FriendlyName.MakeAlphaNumeric() : included.TableName;

                result.AppendLine($"        public DbSet<{EntityModelScript.GetClassName(included)}> {nameToUse} {getset}");
            }

            result.AppendLine();
            result.AppendLine($"        {string.Format(EntityModelScript.Setup.DataContextConstructor, fileName)}");
            result.AppendLine("        {");
            result.AppendLine("        }");
            result.AppendLine();

            result.AppendLine("        protected override void OnModelCreating(DbModelBuilder modelBuilder)");
            result.AppendLine("        {");

            foreach (TableModel table in tablesUsed)
            {
                result.AppendLine($"            modelBuilder.Configurations.Add(new {EntityModelScript.GetClassName(table)}Mapping());");
            }

            foreach (IncludeTableModel included in includeTables)
            {
                if (tablesUsedList.Contains(included.TableName))
                {
                    continue;
                }

                result.AppendLine($"            modelBuilder.Configurations.Add(new {EntityModelScript.GetClassName(included)}Mapping());");
            }

            result.AppendLine("        }");

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

            Paths.CreateDirectory(Path.Combine(EntityModelScript.Setup.ServerOutputBaseDirectory, "DataContext"));

            string outputPath = Path.Combine(EntityModelScript.Setup.ServerOutputBaseDirectory, "DataContext", $"{fileName}.cs");

            File.WriteAllText(outputPath, result.ToString());
        }
        public static string ScriptServerModelMapping(TableModel table)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine(Setup.MappingClassUsing);

            result.AppendLine();

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

            result.AppendLine("{");

            result.AppendLine($"    public class {EntityModelScript.GetClassName(table)}Mapping : EntityTypeConfiguration<{EntityModelScript.GetClassName(table)}>");
            result.AppendLine("    {");

            result.AppendLine($"        public {EntityModelScript.GetClassName(table)}Mapping()");
            result.AppendLine("        {");

            result.AppendLine($"            ToTable(\"{table.TableName}\");");
            result.AppendLine();

            ColumnObjectModel[] pkColumns = table.Columns.Where(pk => pk.InPrimaryKey).ToArray();

            if (pkColumns.HasElements() && pkColumns.Length == 1)
            {
                result.AppendLine($"            HasKey(k => k.{EntityModelScript.GetColumnName(pkColumns[0])});");
            }
            else if (pkColumns.HasElements())
            {
                result.Append("            HasKey(k => new { ");

                for (int x = 0; x < pkColumns.Length; ++x)
                {
                    if (x == (pkColumns.Length - 1))
                    {
                        result.Append($"k.{EntityModelScript.GetColumnName(pkColumns[x])} ");
                    }
                    else
                    {
                        result.Append($"k.{EntityModelScript.GetColumnName(pkColumns[x])}, ");
                    }
                }

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

            result.AppendLine();

            for (int x = 0; x < table.Columns.Length; x++)
            {
                ColumnObjectModel column = table.Columns[x];

                string lambda = $"col{x}";

                result.AppendLine($"            Property({lambda} => {lambda}.{EntityModelScript.GetColumnName(column)}).HasColumnName(\"{column.ColumnName}\");");
            }

            result.AppendLine("        }");

            result.AppendLine("    }");

            result.Append("}");

            return(result.ToString());
        }
 private static string GetModelClassString(TableModel table)
 {
     return(string.Format(EntityModelScript.Setup.ModelClassString, EntityModelScript.GetClassName(table)));
 }