/// <summary> /// Drops all tables matching the name of the LayersCmsDomainObjects to be created, /// then creates fresh tables for those domain objects. /// </summary> private void InitialiseCoreTables(DatabaseSetupConfig config, OrmLiteConnectionFactory dbFactory) { if (String.IsNullOrWhiteSpace(config.UserEmailAddress)) throw new NullReferenceException("No UserEmailAddress specified. Cannot create primary user without an email address."); if (String.IsNullOrWhiteSpace(config.UserPassword)) throw new NullReferenceException("No UserPassword specified. Cannot create primary user without a password."); // Open a database connection using (IDbConnection dbConn = dbFactory.OpenDbConnection()) { // Create the LayersCmsPage table dbConn.DropAndCreateTable<LayersCmsPage>(); // Add the homepage dbConn.Save(new LayersCmsPage() { Active = true, Content = "<p>Welcome to Layers CMS</p>", DisplayName = "Home", PageTitle = "Index", ParentId = null, PublishEnd = null, PublishStart = DateTime.Now.Date, RedirectTypeEnum = RedirectTypeEnum.None, RedirectUrl = null, ShowInNavigation = true, SortOrder = 0, Url = "/", WindowTitle = "Index" }); // Create the LayersCmsUser table and insert the first user dbConn.DropAndCreateTable<LayersCmsUser>(); dbConn.Save(new LayersCmsUser() { Active = true, EmailAddress = config.UserEmailAddress, Password = _hashHelper.HashString(config.UserPassword) // A hashed version of the plain text password }); // Create the settings table and add some default settings dbConn.DropAndCreateTable<LayersCmsSetting>(); dbConn.SaveAll(new [] { new LayersCmsSetting(){SettingType = LayersCmsSettingType.ContactEmailAddress, Value = "*****@*****.**"}, new LayersCmsSetting(){SettingType = LayersCmsSettingType.ContactTelephoneNumber, Value = "0000 000000"}, new LayersCmsSetting(){SettingType = LayersCmsSettingType.GoogleAnalyticsAccountId, Value = ""} }); } }
/// <summary> /// Initialise the database for both the core tables and any bespoke layers (modules) /// </summary> public void InitialiseDatabaseTables(DatabaseSetupConfig config) { // Get the connection string ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings[config.ConnectionStringName]; if (connectionString == null) throw new NullReferenceException("No connection string exists by that key."); // Tell the configuration to use unicode, e.g. nvarchar compared to varchar in SQL Server config.DatabaseDialect.UseUnicode = true; // Initialise the data connection factory var dbFactory = new OrmLiteConnectionFactory(connectionString.ConnectionString, false, config.DatabaseDialect); // Check the config class passed is valid, and we have all we need to complete the setup if (config.DatabaseDialect == null) throw new NullReferenceException("No DatabaseDialect specified. Cannot initialise a database without knowing what type of database to use."); InitialiseCoreTables(config, dbFactory); InitialiseLayers(dbFactory); }
/// <summary> /// Drops all tables matching the name of the LayersCmsDomainObjects to be created, /// then creates fresh tables for those domain objects. /// </summary> public void InitialiseCoreTables(DatabaseSetupConfig config) { // Get the connection string ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings[config.ConnectionStringName]; if (connectionString == null) throw new NullReferenceException("No connection string exists by that key."); // Tell the configuration to use unicode, e.g. nvarchar compared to varchar in SQL Server config.DatabaseDialect.UseUnicode = true; // Initialise the data connection factory var dbFactory = new OrmLiteConnectionFactory(connectionString.ConnectionString, false, config.DatabaseDialect); // Check the config class passed is valid, and we have all we need to complete the setup if (config.DatabaseDialect == null) throw new NullReferenceException("No DatabaseDialect specified. Cannot initialise a database without knowing what type of database to use."); if (String.IsNullOrWhiteSpace(config.UserEmailAddress)) throw new NullReferenceException("No UserEmailAddress specified. Cannot create primary user without an email address."); if (String.IsNullOrWhiteSpace(config.UserPassword)) throw new NullReferenceException("No UserPassword specified. Cannot create primary user without a password."); // Open a database connection using (IDbConnection dbConn = dbFactory.OpenDbConnection()) { // Create the LayersCmsPage table dbConn.DropAndCreateTable<LayersCmsPage>(); // Add the homepage dbConn.Save(new LayersCmsPage() { Active = true, Content = "<p>Welcome to Layers CMS</p>", DisplayName = "Home", PageTitle = "Index", ParentId = null, PublishEnd = null, PublishStart = DateTime.Now.Date, RedirectTypeEnum = RedirectTypeEnum.None, RedirectUrl = null, ShowInNavigation = true, SortOrder = 0, Url = "/", WindowTitle = "Index" }); // Create the LayersCmsUser table and insert the first user dbConn.DropAndCreateTable<LayersCmsUser>(); dbConn.Save(new LayersCmsUser() { Active = true, EmailAddress = config.UserEmailAddress, Password = _hashHelper.HashString(config.UserPassword) // A hashed version of the plain text password }); // Create the LayersCmsImage table dbConn.DropAndCreateTable<LayersCmsImage>(); // Create the settings table and add some default settings dbConn.DropAndCreateTable<LayersCmsSetting>(); dbConn.SaveAll(new [] { new LayersCmsSetting(){SettingType = LayersCmsSettingType.ContactEmailAddress, Value = "*****@*****.**"}, new LayersCmsSetting(){SettingType = LayersCmsSettingType.ContactTelephoneNumber, Value = "0000 000000"}, new LayersCmsSetting(){SettingType = LayersCmsSettingType.GoogleAnalyticsAccountId, Value = ""} }); } }