Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Topic author was deleted

    May 13, 2016 @ 09:31

    Howto use the DatabaseSchemaHelper instead of the DatabaseContext to create tables (during app start)

    Hey, I'm trying to find out how you can use the DatabaseSchemaHelper to check if tables exists and create tables In the past you could do it in the following way

    namespace MyWebsite
    {
        public class RegisterEvents : ApplicationEventHandler
        {
            //This happens everytime the Umbraco Application starts
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                //Get the Umbraco Database context
                var db = applicationContext.DatabaseContext.Database;
    
                //Check if the DB table does NOT exist
                if (!db.TableExist("BlogComments"))
                {
                    //Create DB table - and set overwrite to false
                    db.CreateTable<BlogComment>(false);
                }
    
    
        }
    }
    

    }

    But those methods are now gone ( TableExist and CreateTable) from the Database object and are moved to the DatabaseSchemaHelper (in recent Umbraco versions)

    Now my question is what is the code that works with the DatabaseSchemaHelper instead? I've tried the following but that fails

    public class UmbracoEvents : ApplicationEventHandler
    {
        private readonly UmbracoDatabase _database = ApplicationContext.Current.DatabaseContext.Database;
        private readonly DatabaseSchemaHelper _schemaHelper;
    
        public UmbracoEvents(ISqlSyntaxProvider sqlSyntax, ILogger logger)
        {
    
            _schemaHelper = new DatabaseSchemaHelper(_database, logger, sqlSyntax);
        }
    
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            if (!_schemaHelper.TableExist("sampleContact"))
            {
                //Create DB table - and set overwrite to false
                _schemaHelper.CreateTable<Contact>(false);
            }
    
    
        }
    }
    

    All tips appreciated. Thanks :)

  • Comment author was deleted

    May 13, 2016 @ 09:57

    Tried but also no luck

    public class UmbracoEvents : ApplicationEventHandler
    {
        private readonly UmbracoDatabase _database = ApplicationContext.Current.DatabaseContext.Database;       
    
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var schemaHelper = new DatabaseSchemaHelper(_database, DependencyResolver.Current.GetService<ILogger>(), DependencyResolver.Current.GetService<ISqlSyntaxProvider>());
    
            if (!schemaHelper.TableExist("sampleContact"))
            {
                //Create DB table - and set overwrite to false
                schemaHelper.CreateTable<Contact>(false);
            }
    
            Mapper.CreateMap<ContactViewModel, Contact>();
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft