internal DbContextGenerator(string folder = "") { this.EntityCodeList = new List <string>(); this.directory = string.IsNullOrEmpty(folder) ? Environment.CurrentDirectory : Path.Combine(Environment.CurrentDirectory, folder); IServiceCollection services = new ServiceCollection() .AddEntityFrameworkSqlServer() .AddEntityFrameworkDesignTimeServices() .AddSingleton <ICSharpDbContextGenerator, MyDbContextGenerator>() .AddSingleton <ICSharpEntityTypeGenerator, MyEntityTypeGenerator>() .AddSingleton <IScaffoldingModelFactory, MyScaffoldingModelFactory>(); new SqlServerDesignTimeServices().ConfigureDesignTimeServices(services); var logger = services.GetService <IDiagnosticsLogger <DbLoggerCategory.Scaffolding> >(); var databaseModelFactory = new SqlServerDatabaseModelFactory(logger); MyDbContextGenerator dbContextGenerator = (MyDbContextGenerator)services.GetService <ICSharpDbContextGenerator>(); MyEntityTypeGenerator entityTypeGenerator = (MyEntityTypeGenerator)services.GetService <ICSharpEntityTypeGenerator>(); var scaffoldingModelFactory = (MyScaffoldingModelFactory)services.GetService <IScaffoldingModelFactory>(); var databaseModel = databaseModelFactory.Create(AppSettings.Connection, new List <string>(), new List <string>()); Model model = (Model)scaffoldingModelFactory.Create(databaseModel, false); this.DbContextCode = dbContextGenerator.WriteCode(model, $"{nameof(Quartz)}.{folder}", $"{nameof(Quartz)}Context", AppSettings.Connection, false, false); Helper.FormattingXml(model, databaseModel); foreach (var entityType in model.GetEntityTypes()) { var entityCode = entityTypeGenerator.WriteCode(entityType, $"{nameof(Quartz)}.{folder}", false); this.EntityCodeList.Add(entityCode); } this.WriteCode(scaffoldingModelFactory.Data, $"{nameof(Quartz)}.{folder}"); }
private static DatabaseModel GetDatabaseModel() { var logger = Services.GetService <IDiagnosticsLogger <DbLoggerCategory.Scaffolding> >(); var databaseModelFactory = new SqlServerDatabaseModelFactory(logger); using var connection = new SqlConnection(Connection.ConnectionString); return(databaseModelFactory.Create(connection, new DatabaseModelFactoryOptions(new List <string>(), new List <string>()))); }
public DatabaseModel CreateModel(string createSql, TableSelectionSet selection = null) { _testStore.ExecuteNonQuery(createSql); var reader = new SqlServerDatabaseModelFactory(); return(reader.Create(_testStore.Connection.ConnectionString, selection ?? TableSelectionSet.All)); }
private Dictionary <string, string> ReverseEngineerFiles(ReverseEngineerOptions options, bool useDataAnnotations) { var fileService = new InMemoryTemplateFileService(); fileService.InputFiles(ContextClassTemplate, ContextImportsTemplate, ContextDbSetsTemplate, EntityClassTemplate, EntityImportsTemplate, EntityCtorTemplate, EntityPropertiesTemplate); var dbContextTemplateService = new HbsDbContextTemplateService(fileService); var entityTypeTemplateService = new HbsEntityTypeTemplateService(fileService); ICSharpUtilities cSharpUtilities = new CSharpUtilities(); ICSharpDbContextGenerator realContextGenerator = new HbsCSharpDbContextGenerator( new SqlServerScaffoldingCodeGenerator(), new SqlServerAnnotationCodeGenerator(new AnnotationCodeGeneratorDependencies()), cSharpUtilities, new HbsDbContextTemplateService(fileService)); ICSharpDbContextGenerator contextGenerator = options == ReverseEngineerOptions.DbContextOnly || options == ReverseEngineerOptions.DbContextOnly ? realContextGenerator : new NullCSharpDbContextGenerator(); ICSharpEntityTypeGenerator realEntityGenerator = new HbsCSharpEntityTypeGenerator( cSharpUtilities, new HbsEntityTypeTemplateService(fileService)); ICSharpEntityTypeGenerator entityGenerator = options == ReverseEngineerOptions.EntitiesOnly || options == ReverseEngineerOptions.DbContextAndEntities ? realEntityGenerator : new NullCSharpEntityTypeGenerator(); IScaffoldingCodeGenerator scaffoldingGenerator = new HbsCSharpScaffoldingGenerator( fileService, dbContextTemplateService, entityTypeTemplateService, contextGenerator, entityGenerator); var modelFactory = new SqlServerDatabaseModelFactory( new DiagnosticsLogger <DbLoggerCategory.Scaffolding>( new TestSqlLoggerFactory(), new LoggingOptions(), new DiagnosticListener("Fake"))); var reverseEngineer = new ReverseEngineerScaffolder( modelFactory, new FakeScaffoldingModelFactory(new TestOperationReporter()), scaffoldingGenerator, cSharpUtilities); // Act var files = reverseEngineer.Generate( connectionString: Constants.Connections.SqlServerConnection, tables: Enumerable.Empty <string>(), schemas: Enumerable.Empty <string>(), projectPath: Constants.Parameters.ProjectPath, outputPath: null, rootNamespace: Constants.Parameters.RootNamespace, contextName: Constants.Parameters.ContextName, useDataAnnotations: useDataAnnotations, overwriteFiles: false, useDatabaseNames: false); var generatedFiles = new Dictionary <string, string>(); if (options == ReverseEngineerOptions.DbContextOnly || options == ReverseEngineerOptions.DbContextAndEntities) { var contextPath = files.ContextFile; var context = fileService.RetrieveTemplateFileContents( Path.GetDirectoryName(contextPath), Path.GetFileName(contextPath)); generatedFiles.Add(Constants.Files.DbContextFile, context); } if (options == ReverseEngineerOptions.EntitiesOnly || options == ReverseEngineerOptions.DbContextAndEntities) { var categoryPath = files.EntityTypeFiles[0]; var category = fileService.RetrieveTemplateFileContents( Path.GetDirectoryName(categoryPath), Path.GetFileName(categoryPath)); var productPath = files.EntityTypeFiles[1]; var product = fileService.RetrieveTemplateFileContents( Path.GetDirectoryName(productPath), Path.GetFileName(productPath)); generatedFiles.Add(Constants.Files.CategoryFile, category); generatedFiles.Add(Constants.Files.ProductFile, product); } return(generatedFiles); }
public string GetSchemaSource(string connectionString, string assemblyNamespace, bool withUsings = true) { var loggerFactory = new LoggerFactory().AddConsole(); var ssTypeMap = new Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerTypeMapper(); var ssDbFac = new SqlServerDatabaseModelFactory(loggerFactory: loggerFactory); var ssScaffoldFac = new SqlServerScaffoldingModelFactory( loggerFactory: loggerFactory, typeMapper: ssTypeMap, databaseModelFactory: ssDbFac ); var ssAnnotationProvider = new Microsoft.EntityFrameworkCore.Metadata.SqlServerAnnotationProvider(); var csUtils = new CSharpUtilities(); var scaffUtils = new ScaffoldingUtilities(); var confFac = new ConfigurationFactory( extensionsProvider: ssAnnotationProvider, cSharpUtilities: csUtils, scaffoldingUtilities: scaffUtils ); var fs = new InMemoryFileService(); var sb = new StringBuilderCodeWriter( fileService: fs, dbContextWriter: new DbContextWriter( scaffoldingUtilities: scaffUtils, cSharpUtilities: csUtils ), entityTypeWriter: new EntityTypeWriter(cSharpUtilities: csUtils, scaffoldingUtilities: scaffUtils) ); var rGen = new ReverseEngineeringGenerator( loggerFactory: loggerFactory, scaffoldingModelFactory: ssScaffoldFac, configurationFactory: confFac, codeWriter: sb ); var outputPath = @"C:\temp"; var programName = "Ctx"; var conf = new ReverseEngineeringConfiguration { ConnectionString = connectionString, ContextClassName = programName, ProjectPath = "na", ProjectRootNamespace = assemblyNamespace, OutputPath = outputPath }; var output = new StringBuilder(); var resFiles = rGen.GenerateAsync(conf); resFiles.Wait(); var ctx = CreateContext(fs.RetrieveFileContents(outputPath, programName + ".cs"), isLibrary: withUsings); Console.WriteLine("CreateContext", ctx); if (!withUsings) { ctx = StripHeaderLines(3, ctx); } else { output.Append(_refs); } output.Append(ctx); foreach (var fpath in resFiles.Result.EntityTypeFiles) { output.Append(StripHeaderLines(4, fs.RetrieveFileContents(outputPath, System.IO.Path.GetFileName(fpath)))); } return(output.ToString()); }