public void TestDbToEntitiesSourceGenerator() { var xmlConfigTemplate = @"<Settings> <Provider>@Provider@</Provider> <ConnectionString>@ConnString@</ConnectionString> <OutputPath>_Generated_BookEntities_@[email protected]</OutputPath><!-- Will go into bin folder. --> <Namespace>Vita.Samples.BooksGenerated</Namespace> <AppClassName>BookStoreApp</AppClassName> <Schemas>books</Schemas> <Options>Binary16AsGuid, BinaryKeysAsGuid, AutoOnGuidPrimaryKeys, AddOneToManyLists,GenerateConsoleAppCode,UtcDates</Options> <AutoValues>CreatedOn:CreatedOn</AutoValues> <ForceDataTypes>@ForceDataTypes@</ForceDataTypes> <!-- for SQLite --> </Settings> "; var sqliteForceTypes = "CreatedOn:System.DateTime,CreatedIn:System.Guid,UpdatedIn:System.Guid"; var forceTypes = SetupHelper.ServerType == DbServerType.Sqlite? sqliteForceTypes : string.Empty; var driver = SetupHelper.Driver; var xml = xmlConfigTemplate .Replace("@Provider@", SetupHelper.ServerType.ToString()) .Replace("@ConnString@", SetupHelper.ConnectionString) .Replace("@ForceDataTypes@", forceTypes); var xmlConfig = new XmlDocument(); xmlConfig.LoadXml(xml); var traceFbk = new TraceProcessFeedback(); var dbfirst = new DbFirstProcessor(traceFbk); try { var success = dbfirst.GenerateEntityModelSources(xmlConfig); Assert.IsTrue(success, "Source generation failed."); } catch (Exception ex) { var err = ex.ToLogString(); Debug.WriteLine(err); throw; } }
public bool GenerateEntityModelSources(DbFirstConfig config) { _feedback.WriteLine(" Provider: " + config.ProviderType); _feedback.WriteLine(" Connection string: " + config.ConnectionString); _feedback.WriteLine(); // create driver and check connection string error; if (!DataUtility.TestConnection(config.Driver, config.ConnectionString, out error)) { _feedback.WriteError(error); return(false); } _feedback.WriteLine("Generating entity definitions..."); var appBuilder = new DbFirstAppBuilder(_feedback); var entAppInfo = appBuilder.Build(config); var srcWriter = new DbFirstSourceWriter(_feedback); srcWriter.WriteCsSources(entAppInfo, config); // check errors if (srcWriter.HasErrors) { return(false); } //Everything is ok _feedback.WriteLine("The source code is saved to: " + config.OutputPath); // Do test compile _feedback.WriteLine("Verifying generated code - compiling..."); var genSource = File.ReadAllText(config.OutputPath); var compilerResults = CompilerHelper.CompileSources(config.Driver.GetType(), genSource); if (!compilerResults.Success) { HasErrors = true; _feedback.WriteError("Compile errors detected."); foreach (var err in compilerResults.Messages) { _feedback.WriteError(err); } return(false); } else { //Compare schema _feedback.WriteLine("Verifying generated entities. Running schema comparison ..."); } var nonProcActions = DbFirstProcessor.CompareDatabaseSchemas(config, compilerResults.Assembly); string schemaMessage; if (nonProcActions.Count == 0) { schemaMessage = "Schema verification completed, schemas are identical."; } else { HasErrors = true; schemaMessage = Util.SafeFormat("Schema verification: detected {0} differences (schema update actions).\r\n", nonProcActions.Count); schemaMessage += "Schema update actions:\r\n "; schemaMessage += string.Join("\r\n ", nonProcActions); schemaMessage += @" Note: Non-empty update action list represents the delta between the original database schema and the schema from the generated entities. Ideally this list should be empty. If it is not, then probably some features in your database are not currently supported by VITA. "; } //Dump the message to the source file as comment: System.IO.File.AppendAllText(config.OutputPath, "\r\n/*\r\n" + schemaMessage + "\r\n*/"); _feedback.WriteLine(); _feedback.WriteLine(schemaMessage); return(!HasErrors); }
private static int Run(string[] args) { //Read command line parameters UnpackArguments(args); if(_showHelp) { ShowHelp(); Console.WriteLine(); if(string.IsNullOrEmpty(_configFile)) return 0; } //get config file name if(string.IsNullOrEmpty(_configFile)) { WriteError("Invalid arguments - missing config file parameter (/cfg:<file>)."); ShowHelp(); return -1; //return error } if(!File.Exists(_configFile)) { WriteError("Config file not found: " + _configFile); return -1; //return error } //load config var xmlConfig = LoadConfig(_configFile); var fback = new ConsoleProcessFeedback(); //execute command switch(_command) { case "dbfirst": Console.WriteLine("COMMAND: dbfirst"); Console.WriteLine("Generating entity definitions from the database..."); var dbFirst = new DbFirstProcessor(fback); var success = dbFirst.GenerateEntityModelSources(xmlConfig); return success ? 0 : -1; case "dbupdate": Console.WriteLine("COMMAND: dbupdate"); Console.WriteLine("Generating DB update scripts..."); var dbUpdate = new DbUpdateProcessor(fback); var ok = dbUpdate.GenerateScripts(xmlConfig); return ok ? 0 : -1; default: WriteError(StringHelper.SafeFormat(" Command type arg ({0}) is invalid or missing. Expected 'dbfirst' or 'dbupdate'. ", _command)); ShowHelp(); return -1; } }