Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public bool GenerateEntityModelSources(XmlDocument xmlConfig)
        {
            var config = new DbFirstConfig(xmlConfig);
              _feedback.WriteLine("  Provider: " + config.ProviderType);
              _feedback.WriteLine("  Connection string: " + config.ConnectionString);
              _feedback.WriteLine();

              // create driver and check connection
              string error;
              if(!ToolHelper.TestConnection(config.Driver, config.ConnectionString, out error)) {
            _feedback.WriteError(error);
            return false;
              }

              _feedback.WriteLine("Generating entity definitions...");
              var appBuilder = new DbFirstAppBuilder(_feedback);
              var entApp = appBuilder.Build(config);
              var srcWriter = new DbFirstSourceWriter(_feedback);
              srcWriter.WriteCsSources(entApp, config);
              // check errors
              if(srcWriter.HasErrors)
            return false;
              //Everything is ok
              _feedback.WriteLine("The source code is saved to: " + config.OutputPath);
              // Do test compile
              CompilerResults compilerResults = null;
              _feedback.WriteLine("Verifying generated code - compiling...");
              var genSource = File.ReadAllText(config.OutputPath);
              compilerResults = DbFirstProcessor.CompileSources(config.Driver.GetType(), genSource);
              if(compilerResults.Errors.Count > 0) {
            HasErrors = true;
            _feedback.WriteError("Compile errors detected.");
            foreach(CompilerError err in compilerResults.Errors) {
              var strErr = "  " + err.ErrorText + " Line: " + err.Line;
              _feedback.WriteError(strErr);
            }
            return false;
              } else
              //Compare schema
              _feedback.WriteLine("Verifying generated entities. Running schema comparison ...");

              var nonProcActions = DbFirstProcessor.CompareDatabaseSchemas(config, compilerResults.CompiledAssembly);
              string schemaMessage;
              if(nonProcActions.Count == 0) {
            schemaMessage = "Schema verification completed, schemas are identical.";
              } else {
            HasErrors = true;
            schemaMessage = StringHelper.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;
        }