예제 #1
0
        public EntityApp Build(DbFirstConfig config)
        {
            _config = config;
            _app    = new EntityApp();
            var log = new ActivationLog(_app.ActivationLogPath);

            _dbSettings = new DbSettings(_config.Driver, config.Driver.GetDefaultOptions(), _config.ConnectionString);
            // create loader and setup filter
            var modelLoader = _config.Driver.CreateDbModelLoader(_dbSettings, log);

            modelLoader.SetSchemasSubset(_config.Schemas);
            //actually load model
            _dbModel = modelLoader.LoadModel();
            Util.Check(_dbModel.Tables.Count() > 0, "No tables found in the database. Code generation aborted.");
            // Prepare type generator
            _tempNamespace = "_dummy_" + _callCount++; // artificial unique namespace for dummy interface types
            // Construct model setup and model
            GenerateModulesAndAreas();
            _entityModel = new EntityModel(_app);
            // EntityModelBuilder.SetModel(_app, _entityModel);
            //generate entities and members
            GenerateEntities();
            SetupPrimaryKeys();
            GenerateReferenceMembers();
            CreateIndexes();
            SetupKeyMembers();
            return(_app);
        }
예제 #2
0
        // Used for verification that generated c# entities produce identical database objects
        public static List <DbUpgradeScript> CompareDatabaseSchemas(DbFirstConfig config, Assembly assembly)
        {
            var fullModelName = config.Namespace + "." + config.AppClassName;
            var modelType     = assembly.GetType(fullModelName);

            return(CompareDatabaseSchemas(config, modelType));
        }
예제 #3
0
        public static List <DbUpgradeScript> CompareDatabaseSchemas(DbFirstConfig config, Type modelType)
        {
            var entApp = Activator.CreateInstance(modelType) as EntityApp;

            entApp.EntityClassProvider = new DummyEntityClassProvider();
            entApp.Init();
            // important - do not use DbOptions.AutoIndexForeignKeys - which is recommended for MS SQL, but is not helpful here.
            // This will create a bunch of extra indexes on FKs in entities schema and result in extra differences with original schema.
            //  We ignore stored procs
            var dbOptions  = config.Driver.GetDefaultOptions() & ~DbOptions.AutoIndexForeignKeys;
            var dbSettings = new DbSettings(config.Driver, dbOptions, config.ConnectionString,
                                            upgradeMode: DbUpgradeMode.Always,
                                            upgradeOptions: DbUpgradeOptions.UpdateTables | DbUpgradeOptions.UpdateIndexes
                                            );
            //dbSettings.SetSchemas(config.Schemas);
            var log            = new ActivationLog(null);
            var dbModelBuilder = new DbModelBuilder(entApp.Model, dbSettings.ModelConfig, log);
            var dbModel        = dbModelBuilder.Build();
            var db             = new Database(dbModel, dbSettings);
            var ds             = new DataSource("main", db);
            var upgradeMgr     = new DbUpgradeManager(db, log);
            var upgradeInfo    = upgradeMgr.BuildUpgradeInfo();

            return(upgradeInfo.AllScripts);
        }
예제 #4
0
 public EntityApp Build(DbFirstConfig config)
 {
     _config = config;
       _app = new EntityApp();
       var log = _app.ActivationLog;
       _dbSettings = new DbSettings(_config.Driver, DbOptions.Default, _config.ConnectionString);
       _dbSettings.SetSchemas(_config.Schemas);
       var modelLoader = _config.Driver.CreateDbModelLoader(_dbSettings, log);
       _dbModel = modelLoader.LoadModel();
       Util.Check(_dbModel.Tables.Count() > 0, "No tables found in the database. Code generation aborted.");
       // Prepare type generator
       _tempNamespace = "_dummy_" + _callCount++; // artificial unique namespace for dummy interface types
       // Construct model setup and model
       GenerateModulesAndAreas();
       _entityModel = new EntityModel(_app);
       EntityModelBuilder.SetModel(_app, _entityModel);
       _entityModel.ClassesAssembly = new EntityClassesAssembly();
       //generate entities and members
       GenerateEntities();
       SetupPrimaryKeys();
       GenerateReferenceMembers();
       CreateIndexes();
       SetupKeyMembers();
       return _app;
 }
예제 #5
0
 public void WriteCsSources(EntityApp app, DbFirstConfig config)
 {
     _app = app;
       _config = config;
       HasErrors = false;
       var fileStream = File.Create(_config.OutputPath);
       _output = new StreamWriter(fileStream);
       WriteSource();
       _output.Flush();
       _output.Close();
 }
예제 #6
0
        public void WriteCsSources(EntityApp app, DbFirstConfig config)
        {
            _app      = app;
            _config   = config;
            HasErrors = false;
            var fileStream = File.Create(_config.OutputPath);

            _output = new StreamWriter(fileStream);
            WriteSource();
            _output.Flush();
            _output.Dispose();
        }//method
예제 #7
0
 public static List<DbUpgradeScript> CompareDatabaseSchemas(DbFirstConfig config, Type modelType)
 {
     var entApp = Activator.CreateInstance(modelType) as EntityApp;
       entApp.Init();
       // important - do not use DbOptions.AutoIndexForeignKeys - which is recommended for MS SQL, but is not helpful here.
       // This will create a bunch of extra indexes on FKs in entities schema and result in extra differences with original schema.
       //  We ignore stored procs
       var dbOptions = DbOptions.Default & ~DbOptions.AutoIndexForeignKeys;
       var dbSettings = new DbSettings(config.Driver, dbOptions, config.ConnectionString,
                               upgradeMode: DbUpgradeMode.Always,
                               upgradeOptions : DbUpgradeOptions.UpdateTables | DbUpgradeOptions.UpdateIndexes
                               );
       dbSettings.SetSchemas(config.Schemas);
       var db = new Database(entApp, dbSettings);
       var ds = new DataSource("main", db);
       var upgradeMgr = new DbUpgradeManager(ds);
       var upgradeInfo = upgradeMgr.BuildUpgradeInfo();
       return upgradeInfo.AllScripts;
 }
예제 #8
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);
        }
예제 #9
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;
        }
예제 #10
0
 // Used for verification that generated c# entities produce identical database objects
 public static List<DbUpgradeScript> CompareDatabaseSchemas(DbFirstConfig config, Assembly assembly)
 {
     var fullModelName = config.Namespace + "." + config.AppClassName;
       var modelType = assembly.GetType(fullModelName);
       return CompareDatabaseSchemas(config, modelType);
 }