Copied to clipboard

Flag this post as spam?

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


  • Dan Hammond 7 posts 28 karma points
    Jun 05, 2020 @ 07:44
    Dan Hammond
    1

    How do you create a composite primary key with Umbraco's Migration system?

    I am trying to create a simple table using Umbraco's database migration system, and I can't figure out how to create a table with a composite primary key.

    Here's an example of the code I am trying to use:

    using Umbraco.Core.Migrations;
    using Umbraco.Core.Persistence.DatabaseModelDefinitions;
    using DbConstants = MyProjects.Data.Constants.Database;
    
    namespace MyProjects.Data.Migrations
    {
      public class _00_Create : MigrationBase
      {
        public _00_Create( IMigrationContext context ) : base( context ) { }
    
        public override void Migrate()
        {
          if ( !TableExists( DbConstants.TBL_NAME ) ) {
            Create.Table( DbConstants.TBL_NAME )
              .WithColumn( "Composite1" )
                .AsString( 40 )
                .NotNullable()
                .PrimaryKey( "PK_Composite1" )
              .WithColumn( "Composite2" )
                .AsString( 40 )
                .NotNullable()
                .PrimaryKey("PK_Composite2")
              .WithColumn( "DateAdded" )
                .AsDateTime()
                .WithDefault( SystemMethods.CurrentDateTime )
              .WithColumn( "DateUpdated" )
                .AsDateTime()
                .WithDefault( SystemMethods.CurrentDateTime )
              .Do();
          }
        }
      }
    }
    

    The above is only an example of what I'm trying to do, hence the rubbish column naming.

    I am aware that there's a way to pass a type into the Create.Table method instead, but I have read that it's inadvisable to use that method with migrations.

    Is someone able to explain how to create such a table using Umbraco Migrations?

    Thanks

  • Dan Hammond 7 posts 28 karma points
    Jun 08, 2020 @ 07:26
    Dan Hammond
    0

    Sorry to bump this, but I still haven't figured it out.

    Could anyone advise?

    Thanks

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies