public void TableCounTest() { using (var schemaReader = SchemaReaderProvider.GetReader(DbServerType.MsSql)) { Tables tables = schemaReader.ReadSchema(_connectionString); Assert.NotNull(schemaReader); Assert.NotNull(tables); Assert.Equal(11, tables.Count); } }
public static void Generate(ReaderParameters readerParameters) { using (var schemaReader = SchemaReaderProvider.GetReader(readerParameters.ServerType)) { var connectionString = readerParameters.ConnectionString; if (readerParameters.UseConnectionStringName) { connectionString = ConfigurationManager.ConnectionStrings[readerParameters.ConnectionStringName].ConnectionString; } if (schemaReader == null || string.IsNullOrEmpty(connectionString)) { throw new Exception("Schema reader object or connection ring not valid!"); } if (string.IsNullOrEmpty(readerParameters.Namespace)) { readerParameters.Namespace = readerParameters.ServerType.ToString() + ".Models"; } if (string.IsNullOrEmpty(readerParameters.ModelsLocation)) { readerParameters.ModelsLocation = readerParameters.ServerType.ToString() + ".Models"; } var tables = schemaReader.ReadSchema(connectionString); foreach (var table in tables) { var model = new ModelTemplate(); model.Namespace = readerParameters.Namespace; model.IncludeRelationships = readerParameters.IncludeRelationships; model.Table = table; model.Tables = tables; // get page content string pageContent = model.TransformText(); if (!Directory.Exists(readerParameters.ModelsLocation)) { Directory.CreateDirectory(readerParameters.ModelsLocation); } // Write model to file string fileName = table.ClassName + ".cs"; Console.WriteLine(string.Format("Creating file {0} ...", fileName)); File.WriteAllText(Path.Combine(readerParameters.ModelsLocation, fileName), pageContent); } } }
public static Task <GeneratePocoResponse> Generate(GeneratePocoRequest request) { return(Task.Run(() => { using (var schemaReader = SchemaReaderProvider.GetReader(request.ServerType)) { var connectionString = request.ConnectionString; if (schemaReader == null || string.IsNullOrEmpty(connectionString)) { return new GeneratePocoResponse(); } var tables = schemaReader.ReadSchema(connectionString); if (tables == null || tables.Count <= 0) { throw new Exception(string.Format("Empty database or invalid connection string: {0}", connectionString)); } var fileNames = new List <string>(); foreach (var table in tables) { var model = new ModelTemplate(); model.Namespace = request.Namespace; model.IncludeRelationships = request.IncludeRelationship; model.Table = table; model.Tables = tables; // get page content string pageContent = model.TransformText(); if (!Directory.Exists(request.FolderLocation)) { Directory.CreateDirectory(request.FolderLocation); } // Write model to file string fileName = table.ClassName + ".cs"; File.WriteAllText(Path.Combine(request.FolderLocation, fileName), pageContent); fileNames.Add(fileName); } var response = new GeneratePocoResponse(); response.Filenames = fileNames; return response; } })); }
public void CreateModelsTest() { using (var schemaReader = SchemaReaderProvider.GetReader(DbServerType.MsSql)) { Tables tables = schemaReader.ReadSchema(_connectionString); Assert.NotNull(schemaReader); Assert.NotNull(tables); var fileFullPathList = new List <string>(); foreach (var table in tables) { var model = new ModelTemplate(); model.Namespace = Namespace; model.IncludeRelationships = true; model.Table = table; model.Tables = tables; // get page content string pageContent = model.TransformText(); // your generated model files go here string modelsPath = Namespace; if (!Directory.Exists(modelsPath)) { Directory.CreateDirectory(modelsPath); } // Write model to file string fileFullPath = string.Format("{0}\\{1}", modelsPath, table.ClassName) + ".cs"; File.WriteAllText(fileFullPath, pageContent); fileFullPathList.Add(fileFullPath); } // Check if files exits Assert.Equal(tables.Count, fileFullPathList.Count); foreach (var fileFullpath in fileFullPathList) { Assert.True(File.Exists(fileFullpath)); } } }
static void Main(string[] args) { using (var schemaReader = SchemaReaderProvider.GetReader(DbServerType.MySql)) { var connectionString = ConfigurationManager.ConnectionStrings["MySqlSugarCrmConnectionString"].ConnectionString; if (schemaReader == null || string.IsNullOrEmpty(connectionString)) { // return new GeneratePocoResponse(); } var tables = schemaReader.ReadSchema(connectionString); if (tables == null || tables.Count <= 0) { throw new Exception(string.Format("Empty database or invalid connection string: {0}", connectionString)); } var fileNames = new List <string>(); foreach (var table in tables) { var model = new ModelTemplate(); model.Namespace = "Sugar.PocoGen.Console.Models"; model.IncludeRelationships = true; model.Table = table; model.Tables = tables; // get page content string pageContent = model.TransformText(); string folder = "Models"; if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } // Write model to file string fileName = table.ClassName + ".cs"; File.WriteAllText(Path.Combine(folder, fileName), pageContent); fileNames.Add(fileName); } } }
public static Task <GenerateCodeResponse> Generate(GenerateCodeRequest request) { return(Task.Run(() => { using (var schemaReader = SchemaReaderProvider.GetReader(request.ServerType)) { var connectionString = request.ConnectionString; if (schemaReader == null || string.IsNullOrEmpty(connectionString)) { return new GenerateCodeResponse(); } var tables = schemaReader.ReadSchema(connectionString); if (tables == null || tables.Count <= 0) { throw new Exception(string.Format("Empty database or invalid connection string: {0}", connectionString)); } var response = new GenerateCodeResponse { Filenames = new List <string>() }; var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(p => typeof(ITemplate).IsAssignableFrom(p)); foreach (var type in types) { var attr = (OutputFileNameAttribute)type.GetCustomAttributes(typeof(OutputFileNameAttribute), true).FirstOrDefault(); if (attr != null) { typeof(GenerateCodeProcessor).GetMethod("GenerateCode") .MakeGenericMethod(type) .Invoke(null, new object[] { tables, request, response, type.Name, attr.FileName }); } } return response; } })); }