Copied to clipboard

Flag this post as spam?

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


  • Giu 23 posts 141 karma points
    Jan 04, 2018 @ 13:24
    Giu
    0

    Umbraco Migration process: Error with MigrationBase class

    Hi, I am using the 7.7.7 version and I wanted to add columns for one of my custom tables in my Umbraco project. As I did that before in 7.6.8, I copy the code I have:

     public class TestFeatureMigration : MigrationBase
    {
        public TestFeatureMigration (ISqlSyntaxProvider sqlSyntax, ILogger logger) : base(sqlSyntax, logger)
        {
    
        }
    
        public override void Down()
        {
            Delete.FromTable("table1").Row("row1");
        }
    
        public override void Up()
        {
            Alter.Table("table1").AddColumn("row1").AsInt32().Nullable();
        }
    }
    

    And when I call this class using an ApplicationEventHandler, I get this weird exception:

    Logger: Umbraco.Core.PluginManager

    Error creating type TestFeatureMigration System.MissingMethodException: No parameterless constructor defined for this object.

    So it's a reflection issue and umbraco is expecting an empty constructor for TestFeatureMigration. The problem is that MigrationBase is requesting a constructor with those parameters.

    Is it a breaking change between 7.6 and 7.7 ? Am I using MigrationBase wrong ?

  • Giu 23 posts 141 karma points
    Feb 01, 2018 @ 16:04
    Giu
    0

    I found the issue, it looks like it's not possible to have 2 classes inheriting from MigrationBase at the same time in the same project.

  • Kevin Jump 2332 posts 14859 karma points MVP 8x c-trib
    Feb 01, 2018 @ 17:03
    Kevin Jump
    101

    Hi,

    you can have multiple classes inheriting from migration base, it was probibly more likely that the default constructor when you overrode the class was protected.

    this catches me out all the time :( when you inherit the class in visual studio the default constructor will be protected.

      public class Testmigration : MigrationBase
        {
            protected Testmigration(ISqlSyntaxProvider sqlSyntax, ILogger logger) : base(sqlSyntax, logger)
            {
            }
    

    you just need to change protected to public.

  • Lee Kelleher 4025 posts 15835 karma points MVP 13x admin c-trib
    Feb 22, 2018 @ 18:34
    Lee Kelleher
    0

    Thank you Kevin! I've been scratching my head for the past few hours trying to figure out what was going wrong... and yup, the constructor was protected! (facepalm)

    Cheers,
    - Lee

  • Giu 23 posts 141 karma points
    Feb 01, 2018 @ 17:21
    Giu
    0

    That makes sense, it explains the reflection error, thanks mate

  • Angel 54 posts 111 karma points
    Aug 14, 2024 @ 19:33
    Angel
    0

    I am still having this issue, even though my constructor of my class is public.

    The entry is written into the migration db table, but the error in the log is:

    System.MissingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at Umbraco.Core.Persistence.Migrations.MigrationResolver.MigrationServiceProvider.GetService(Type serviceType) at Umbraco.Core.ServiceProviderExtensions.CreateInstances[T](IServiceProvider serviceProvider, IEnumerable`1 types, ILogger logger, Boolean throwException)

    [Migration("0.0.1", 1, "MyProject.Migrations")]
        public class MultiUrlPickerMigration : MigrationBase
        {
    
            private readonly ILogger _logger;
    
            public MultiUrlPickerMigration(ISqlSyntaxProvider sqlSyntax, ILogger logger)
            : base(sqlSyntax, (Umbraco.Core.Logging.ILogger)logger)
            {
                _logger = logger;
            }
    
    
    
            public override void Up()
            {
    
            }
    
            public override void Down()
            {
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft