Copied to clipboard

Flag this post as spam?

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


  • Mark Drake 80 posts 318 karma points c-trib
    Jul 14, 2021 @ 02:01
    Mark Drake
    0

    Adding a Custom Database Table to Umbraco 9

    I'm trying to replicate some work I did for Umbraco 8 in Umbraco 9. So naturally, I went to our documentation and pulled up what I thought would have been current for v9.


    I believe there is an issue with the upgrader.Execute method.

    It actually doesn't take the same parameters as described in the docs. It actually looks more like this:

    var migrationPlanExecutor = new MigrationPlanExecutor(_scopeProvider, _loggerFactory, _migrationBuilder);
    var upgrader = new Upgrader(migrationPlan);
    upgrader.Execute(
        migrationPlanExecutor,
        _scopeProvider,
        _keyValueService);
    

    What I need help with is a Visual Studio reported error for MigrationBase.Migrate.

    CS0507: cannot change access modifiers when overriding 'protected' inherited member 'MigrationBase.Migrate()'.
    

    Can anyone with backend experience explain what changed and what the solution should be? Much appreciated!

  • Mark Drake 80 posts 318 karma points c-trib
    Jul 14, 2021 @ 02:39
    Mark Drake
    101

    Sorry, I'm definitely not a backend programmer. When I was given the error that this was protected, I had no idea that I could still override it? Seems weird to me, but I was able to rewrite my code as follows:

    public class AddContentNodeIconsTable : MigrationBase
    {
        public AddContentNodeIconsTable(IMigrationContext context) : base(context)
        {
        }
    
        protected override void Migrate()
        {
            Logger.LogDebug("Running migration {MigrationStep}", "AddHumbleContentNodeIconsTable");
    
            // Lots of methods available in the MigrationBase class - discover with this.
            if (TableExists("Humble_ContentNodeIcons") == false)
            {
                Create.Table<Schema>().Do();
            }
            else
            {
                Logger.LogDebug("The database table {DbTable} already exists, skipping", "HumbleContentNodeIcons");
            }
        }
    
    }
    
  • Andy Butland 395 posts 2231 karma points MVP 4x hq c-trib
    Jul 14, 2021 @ 05:35
    Andy Butland
    101

    You found the answer before I could reply, but yes, in a recent change just before the RC this method on the base class MigrationBase was changed from public to protected. And when you override a method in C#, you can't change it's "access modifier" - which is one of public, protected, private or internal.

    And if it helps with a little background, the ability to override a method is given by it being defined as virtual on the base class, which means you can then apply override as you have. The access modifiers are a separate concept, controlling where your method can be called from other code.

    So in this case "protected" doesn't actually mean "you can't override" as you might have originally thought. Rather it means "you can access this method only in the current class or anything that inherits from it, but not from another unrelated class".

    Andy

  • Mark Drake 80 posts 318 karma points c-trib
    Jul 14, 2021 @ 16:56
    Mark Drake
    0

    You're awesome Andy. Thank you for explaining to me what protected actually means in this context. It makes much more sense now.

  • Mark Drake 80 posts 318 karma points c-trib
    Jul 14, 2021 @ 19:02
    Mark Drake
    0

    This nuget package stuff is still wizardry to me. Could someone let me know if this package works for them? Requires the pre-release version of Umbraco (v9.0.0-rc001).

    https://www.nuget.org/packages/Humble.ContentNodeIcons/9.0.0-rc001

Please Sign in or register to post replies

Write your reply to:

Draft