Copied to clipboard

Flag this post as spam?

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


  • Martin Griffiths 826 posts 1269 karma points c-trib
    Dec 31, 2020 @ 10:42
    Martin Griffiths
    0

    Umbraco Migrations v7.15.x

    Hello everyone in the fabulous Umbraco community. I hope you have had a safe and happy holiday (Christmas for those of you that celebrate it) and my best wishes go out to you all for the new year.

    I'm kicking off 2021 with a major upgrade of our main corporate website to Umbraco 8. Cloud is also on the cards, but first I have a ton of legacy data to contend with.

    I have successfully managed to get Migrations working but was wondering why the Database method is no longer working in the ApplicationContext. I have been repeatedly getting a object reference error, but the inner exception doesn't shed any light on what's causing it. This error prevents a database entry of the product-name-upgrade and semversion in the migrations table.

    To remedy the problem I have used the UmbracoDatabase object to new up an instance of an Umbraco Database and this seems to work fine, but it took days of searching and digging around before I found this. So it would seem this is not a widely known or documented issue.

    As with all things Umbraco, over the years I have found setups, configurations and code can vary wildly from project-to-project and version-to-version, so I'm happy to accept it could be my project that is at fault. So here are some tidbits of information about my own project that may shed some light on my issue.

    Umbraco version 7.15.6 Autofac enabled and in-use ModelsBuilder enabled and in-use

    Local build VS2019/MSSQL-LocalDB/IIS Express

    Here's my event handler code that is working, using the alternative approach I stumbled across but isn't widely documented. Incidentally, The Our.Umbraco.Migrations project suffers the same issue as it too calls the Database method from ApplicationContext.

    UmbracoDatabase db = new UmbracoDatabase(applicationContext.DatabaseContext.ConnectionString, Constants.DatabaseProviders.SqlServer, 
                                ApplicationContext.Current.ProfilingLogger.Logger);
    
            const string productName = "PromotionModuleDoctypeMigration";
            SemVersion currentVersion = new SemVersion(0, 0, 0);
    
            IEnumerable<IMigrationEntry> migrations = ApplicationContext.Current.Services.MigrationEntryService.GetAll(productName);
            IMigrationEntry latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();
    
            if (latestMigration != null)
                currentVersion = latestMigration.Version;
    
            SemVersion targetVersion = new SemVersion(1, 0, 0);
    
            if (targetVersion == currentVersion)
                return;
    
            MigrationRunner migrationsRunner = new MigrationRunner(
              ApplicationContext.Current.Services.MigrationEntryService,
              ApplicationContext.Current.ProfilingLogger.Logger,
              currentVersion,
              targetVersion,
              productName);
    
            try
            {
                migrationsRunner.Execute(db);
            }
            catch (Exception e)
            {
                LogHelper.Error<MigrationEvents>("Error running PromotionModuleDoctypeMigration migration", e);
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft