public DataConnection(EntitySession session, Database database, ConnectionLifetime lifetime, bool admin = false) { Session = session; Database = database; Lifetime = lifetime; var connString = admin ? database.Settings.SchemaManagementConnectionString : database.Settings.ConnectionString; DbConnection = database.DbModel.Driver.CreateConnection(connString); }
public DataSource(string name, Database database, CacheSettings cacheSettings = null) { Database = database; App = Database.DbModel.EntityApp; Name = database.Settings.DataSourceName; if(cacheSettings != null && cacheSettings.HasTypes()) Cache = new EntityCache(App, cacheSettings, this.Database); }
public DbBatchCommandSetBuilder(Database db, UpdateSet updateSet) { _db = db; _driver = _db.DbModel.Driver; _updateSet = updateSet; // Clear identity holders in record.CustomTag if (_updateSet.UsesOutParams) foreach (var rec in updateSet.AllRecords) rec.CustomTag = null; }
public EntityCache(EntityApp app, CacheSettings settings, Database database) { _app = app; Settings = settings; _dataStore = database; _sparseCache = new SparseEntityCache(Settings); var dbIsCaseInsensitive = database.Settings.Driver.Features.IsSet(Data.Driver.DbFeatures.DefaultCaseInsensitive); var caseMode = dbIsCaseInsensitive ? StringCaseMode.CaseInsensitive : StringCaseMode.CaseSensitive; _fullSetCache = new FullSetEntityCache(_app, Settings, _dataStore, caseMode); _timeService = _app.GetService<ITimeService>(); _logService = _app.GetService<IOperationLogService>(); _errorLog = _app.GetService<IErrorLogService>(); MarkCachedEntities(); _app.AppEvents.SavedChanges += Events_SavedChanges; }
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; }
public bool GenerateScripts(XmlDocument xmlConfig) { const string header = @"-- DDL Scripts generated by VITA DB Tool. -- Generated on: {0} -- Target database: {1} -- Executed by user {2} on machine {3}. "; _config = new DbUpdateConfig(xmlConfig); Util.Check(File.Exists(_config.AssemblyPath), "Assembly file '{0}' not found.", _config.AssemblyPath); var asm = Assembly.LoadFrom(_config.AssemblyPath); var appType = asm.GetType(_config.AppClassName); Util.Check(appType != null, "Type {0} not found in target assembly."); // Using NonPublic flag to allow internal constructor; // EntityApp must have a parameterless constructor, but it may be made internal, to hide from regular code var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; var appObj = Activator.CreateInstance(appType, flags, null, null, null); Util.Check(appObj != null, "Failed to create instance of class {0}.", _config.AppClassName); var entApp = appObj as EntityApp; Util.Check(entApp != null, "The target instance of class {0} is not an EntityApp instance.", _config.AppClassName); entApp.Init(); var dbSettings = new DbSettings(_config.Driver, _config.DbOptions, _config.ConnectionString, upgradeMode: DbUpgradeMode.Always, upgradeOptions: _config.ModelUpdateOptions); var schemas = entApp.Areas.Select(a => a.Name).ToList(); dbSettings.SetSchemas(schemas); var db = new Database(entApp, dbSettings); var ds = new DataSource(null, db); var updateMgr = new DbUpgradeManager(ds); var upgrades = updateMgr.BuildUpgradeInfo(); var ddl = string.Join(_config.Driver.DDLSeparator, upgrades.AllScripts.Select(scr => scr.Sql)); if (string.IsNullOrEmpty(ddl)) ddl = "-- (No changes detected)"; var text = string.Format(header, DateTime.Now.ToString("s"), _config.ConnectionString, Environment.UserName, Environment.MachineName) + ddl; File.WriteAllText(_config.OutputPath, text); _feedback.WriteLine(" Generated {0} scripts.", upgrades.AllScripts.Count); _feedback.WriteLine(" DDL scripts are saved to '{0}'", _config.OutputPath); return true; }
public bool UpdateDbInfo(Database db, Exception exception = null) { //Check that db has module's tables; if not, this module is not included in the solution var tbl = db.DbModel.GetTable(typeof(IDbInfo)); if (tbl == null) return false; try { var app = db.DbModel.EntityApp; var session = App.OpenSystemSession(); // Disable stored procs and disable batch mode session.DisableStoredProcs(); session.DisableBatchMode(); var ent = session.GetEntities<IDbInfo>(take: 1).FirstOrDefault(e => e.AppName == app.AppName); if(ent == null) { ent = session.NewEntity<IDbInfo>(); ent.Version = app.Version.ToString(); ent.AppName = app.AppName; } if(exception == null) { ent.Version = app.Version.ToString(); ent.LastModelUpdateFailed = false; ent.LastModelUpdateError = null; ent.Values = SerializeValues(db.DbModel.VersionInfo); SaveModulesInfo(session, db.DbModel.VersionInfo); } else { ent.LastModelUpdateFailed = true; ent.LastModelUpdateError = exception.ToLogString(); } // we use db.SaveChanges directly, to make sure we go thru proper database var entSession = (Vita.Entities.Runtime.EntitySession)session; db.SaveChanges(entSession); return true; } catch (Exception ex) { App.ActivationLog.Error(ex.ToLogString()); return false; } }