Exemplo n.º 1
0
        private void GenerateDbModel()
        {
            var dt       = GetTableFlat();
            var nameText = Namespace ?? "Aescr.Model";

            for (int index = 0; index < dt.Rows.Count;)
            {
                DataRow dataRow = dt.Rows[index];
                var     ft      = CreateFile();
                ft.DownloadPath = DownloadPath + "/DbModel";
                var namespaceTemplate = ft.CreateNamespace();
                namespaceTemplate.NamespaceName = nameText;
                ClassTemplate classTemplate = new ClassTemplate();
                namespaceTemplate.AddClass(classTemplate);
                if (RemoveLine)
                {
                    classTemplate.ClassName = dataRow["表名"].ToString();
                    classTemplate.ClassName = classTemplate.ClassName.Replace("_", "");
                }
                else
                {
                    classTemplate.ClassName = dataRow["表名"].ToString();
                }
                classTemplate.RealName            = dataRow["表名"].ToString();
                classTemplate.Comment             = new Code.Template.CommentTemplate();
                classTemplate.Comment.CommentName = dataRow["表说明"].ToString();
                do
                {
                    DataRow tempRow = dt.Rows[index];
                    var     field   = new Code.Template.FieldTemplate();
                    var     comment = new Code.Template.CommentTemplate();
                    field.FieldName     = tempRow["字段名"].ToString();
                    comment.CommentName = tempRow["字段说明"].ToString();
                    field.DbType        = tempRow["类型"].ToString();
                    field.MaxLength     = tempRow["长度"].ToString().ToInt64();
                    field.MinLength     = 0;
                    field.Comment       = comment;
                    field.IsProperty    = true;
                    field.IsKey         = tempRow["主键"].ToString() == "√";
                    field.CanNull       = tempRow["允许空"].ToString() == "√";
                    if (_connect.DbType == DataBaseType.MsSQL)
                    {
                        field.FieldTypeName = DbToCsharpType.MsSqlToCsharpType(tempRow["类型"].ToString());
                    }
                    else if (_connect.DbType == DataBaseType.MySQL)
                    {
                        field.FieldTypeName = DbToCsharpType.MySqlToCsharpType(tempRow["类型"].ToString());
                    }
                    classTemplate.AddField(field);
                    index++;
                    if (index == dt.Rows.Count)
                    {
                        break;
                    }
                } while (string.IsNullOrWhiteSpace(dt.Rows[index]["表名"].ToString()));
            }
        }
Exemplo n.º 2
0
        private void GenerateDbInputModel()
        {
            var dt       = _dbTable;
            var nameText = RepositoryNamespace ?? "Aescr.Model";

            for (int index = 0; index < dt.Rows.Count;)
            {
                DataRow dataRow   = dt.Rows[index];
                var     inputFile = CreateFile();
                inputFile.DownloadPath = DownloadPath + "/InputModel";
                var viewFile = CreateFile();
                viewFile.DownloadPath = DownloadPath + "/ViewModel";
                var updateFile = CreateFile();
                updateFile.DownloadPath = DownloadPath + "/InputModel";
                var namespaceTemplate = inputFile.CreateNamespace();
                namespaceTemplate.NamespaceName = nameText;
                var viewTemplate = namespaceTemplate.DeepClone();
                viewFile.AddNamespace(viewTemplate);
                var updateTemplate = namespaceTemplate.DeepClone();
                updateFile.AddNamespace(updateTemplate);
                ClassTemplate classTemplate = new ClassTemplate();
                classTemplate.ClassName           = dataRow["表名"].ToString();
                classTemplate.ClassName           = classTemplate.ClassName.Replace("_", "");
                classTemplate.RealName            = dataRow["表名"].ToString();
                classTemplate.Comment             = new Code.Template.CommentTemplate();
                classTemplate.Comment.CommentName = dataRow["表说明"].ToString();
                do
                {
                    DataRow tempRow = dt.Rows[index];
                    var     field   = new Code.Template.FieldTemplate();
                    var     comment = new Code.Template.CommentTemplate();
                    field.FieldName     = tempRow["字段名"].ToString();
                    comment.CommentName = tempRow["字段说明"].ToString();
                    field.DbType        = tempRow["类型"].ToString();
                    field.MaxLength     = tempRow["长度"].ToString().ToInt64();
                    field.MinLength     = 0;
                    field.Comment       = comment;
                    field.IsProperty    = true;
                    field.IsKey         = tempRow["主键"].ToString() == "√";
                    field.CanNull       = tempRow["允许空"].ToString() == "√";
                    if (_dbGenerate.DbType == DataBaseType.MsSQL)
                    {
                        field.FieldTypeName = DbToCsharpType.MsSqlToCsharpType(tempRow["类型"].ToString());
                    }
                    else if (_dbGenerate.DbType == DataBaseType.MySQL)
                    {
                        field.FieldTypeName = DbToCsharpType.MySqlToCsharpType(tempRow["类型"].ToString());
                    }

                    if (field.CanNull == false)
                    {
                        classTemplate.AddField(field);
                        var inputClass  = classTemplate.DeepClone();
                        var viewClass   = classTemplate.DeepClone();
                        var updateClass = classTemplate.DeepClone();
                        inputClass.ClassName = "Input" + inputClass.ClassName;
                        namespaceTemplate.AddClass(inputClass);
                        viewClass.ClassName = "View" + viewClass.ClassName;
                        viewTemplate.AddClass(viewClass);
                        updateClass.ClassName = "Update" + updateClass.ClassName;
                        updateTemplate.AddClass(updateClass);
                    }
                    index++;
                    if (index == dt.Rows.Count)
                    {
                        break;
                    }
                } while (string.IsNullOrWhiteSpace(dt.Rows[index]["表名"].ToString()));
            }
        }
Exemplo n.º 3
0
        private void GenerateDbContext()
        {
            var ft = CreateFile();

            ft.DownloadPath = DownloadPath + "/DbContext";
            var namespaceTemplate = ft.CreateNamespace();

            namespaceTemplate.NamespaceName = "Microsoft.EntityFrameworkCore";
            var classTemplate = new ClassTemplate();

            namespaceTemplate.AddClass(classTemplate);
            classTemplate.ClassName = _connect.Database.Trim() + "DbContext";
            classTemplate.BaseClass = new ClassTemplate()
            {
                ClassName = "DbContext"
            };
            var method = new MethodTemplate();

            classTemplate.AddMethod(method);
            method.Overwrite  = true;
            method.MethodName = "OnModelCreating";
            method.Parameters.Add(new ParameterTemplate()
            {
                ParameterTypeName = "ModelBuilder",
                ParameterName     = "modelBuilder"
            });
            var dt = GetTableFlat();

            for (int index = 0; index < dt.Rows.Count;)
            {
                DataRow       dataRow       = dt.Rows[index];
                ClassTemplate tableTemplate = new ClassTemplate();
                if (RemoveLine)
                {
                    tableTemplate.ClassName = dataRow["表名"].ToString();
                    tableTemplate.ClassName = classTemplate.ClassName.Replace("_", "");
                }
                else
                {
                    tableTemplate.ClassName = dataRow["表名"].ToString();
                }

                tableTemplate.RealName            = dataRow["表名"].ToString();
                tableTemplate.Comment             = new Code.Template.CommentTemplate();
                tableTemplate.Comment.CommentName = dataRow["表说明"].ToString();
                method.CodeLine.Add($"modelBuilder.Entity<{ tableTemplate.ClassName}>().HasComment(\"{tableTemplate.Comment.CommentName  }\");");
                var field = new Code.Template.FieldTemplate();
                field.IsGenerateAttribute = false;
                field.FieldName           = tableTemplate.ClassName;
                field.FieldTypeName       = $"DbSet<{tableTemplate.ClassName}>";
                field.IsProperty          = true;
                classTemplate.AddField(field);
                do
                {
                    DataRow tempRow = dt.Rows[index];
                    method.CodeLine.Add($"modelBuilder.Entity<{  tableTemplate.ClassName }>().Property(b => b.{ tempRow["字段名"].ToString()}).HasComment(\"{  tempRow["字段说明"].ToString()}\");");
                    index++;
                    if (index == dt.Rows.Count)
                    {
                        break;
                    }
                } while (string.IsNullOrWhiteSpace(dt.Rows[index]["表名"].ToString()));
            }
        }