Copied to clipboard

Flag this post as spam?

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


  • Tim Geyssens 6419 posts 15026 karma points MVP 2x c-trib
    Jan 02, 2019 @ 09:36
    Tim Geyssens
    0

    V8 - Create table based on poco with attributes

    Having a go with v8 but noticed that some of the db method have gone private so not sure how to do the following:

    https://creativewebspecialist.co.uk/2013/07/16/umbraco-petapoco-to-store-blog-comments/

    if (!db.TableExist("BlogComments"))
            {
                //Create DB table - and set overwrite to false
                db.CreateTable<BlogComment>(false);
            }
    
  • Jan van Helvoort 17 posts 185 karma points c-trib
    Jan 02, 2019 @ 10:13
    Jan van Helvoort
    1

    If you use migrations, you could use something like this:

    public class InitialMigration : MigrationBase
    {
        public InitialMigration(IMigrationContext context) : base(context)
        {
    
        }
    
        public override void Migrate()
        {
            if (!this.TableExists("BlogComments"))
            {
                this.Create.Table<BlogComment>().Do();
            }
        }
    }
    

    Stephan has write a blog about migrations in V8 - https://www.zpqrtbnk.net/posts/migrations-in-v8

  • Tim Geyssens 6419 posts 15026 karma points MVP 2x c-trib
    Jan 02, 2019 @ 10:33
    Tim Geyssens
    0

    thanks, will give it a go!

  • Søren Kottal 419 posts 2532 karma points MVP c-trib
    Jan 02, 2019 @ 12:55
    Søren Kottal
    2

    Hi Jan

    I think you need to add .Do() to your Create.Table call.

    And you can get the expected table name from NPoco by writing TableInfo.FromPoco(typeof(BlogComment)).TableName

    So the Migrate method would be like this:

    public override void Migrate()
    {
        if (!TableInfo.FromPoco(typeof(BlogComment)).TableName)
        {
            Create.Table<BlogComment>().Do();
        }
    }
    

    That is how I got it working :)

  • Jan van Helvoort 17 posts 185 karma points c-trib
    Jan 02, 2019 @ 13:02
    Jan van Helvoort
    0

    Hi Søren,

    Thank you, this code below I used for playing with Umbraco and works. I see that I missed the .Do() in the post above, I will change it.

            if (!this.TableExists(TableConstants.Tasks.TableName))
            {
                this.Create.Table(TableConstants.Tasks.TableName)
                    .WithColumn("Id").AsInt16().NotNullable()
                        .PrimaryKey("PK_tasks").Identity()
                    .WithColumn("Content").AsString().NotNullable()
                    .WithColumn("IsReady").AsBoolean().NotNullable()
                    .Do();
            }
    
  • Tim Geyssens 6419 posts 15026 karma points MVP 2x c-trib
    Jan 02, 2019 @ 13:25
    Tim Geyssens
    0

    Thanks for the extra info Søren

  • Tim Geyssens 6419 posts 15026 karma points MVP 2x c-trib
    Jan 02, 2019 @ 13:29
    Tim Geyssens
    0

    And if I don't want to run this from a migration (since it needs to run on every app start), could I also execute that code from a component? https://creativewebspecialist.co.uk/2018/06/15/umbraco-v8-bye-bye-applicationeventhandler-hello-umbraco-components/

  • Warren Buckley 2085 posts 4501 karma points MVP 6x admin hq c-trib
    Jan 02, 2019 @ 20:44
    Warren Buckley
    0

    Tim,
    Any reason you need to run SQL everytime the app boots?

    As everyone has mentioned, using migrations is the way to go for this.

    With migrations it will save you this work, if an envionment such as local to development or development to live, has not run a migration (the current latest state of a migration is stored)

    So if the Live site does not have migration-c but has migration-a in its DB it will go through migrations migration-b & migration-c

    So migrations is the way to deal with SQL/Table stuff. Look at V8 core and how many migrations we have just for V8.0.0 https://github.com/umbraco/Umbraco-CMS/tree/temp8/src/Umbraco.Core/Migrations/Upgrade/V80_0

    And take a look at what we do again in V8 core to ensure we go through all the steps/migrations in a specific order https://github.com/umbraco/Umbraco-CMS/blob/temp8/src/Umbraco.Core/Migrations/Upgrade/UmbracoPlan.cs

    Hope this helps.

    Cheers,
    Warren

  • Warren Buckley 2085 posts 4501 karma points MVP 6x admin hq c-trib
    Jan 02, 2019 @ 21:15
    Warren Buckley
    0

    But if you need or want to use SQL/NPoco (The PetaPoco replacement) you could use something like this.

    Note the scope.complete() without it, the transaction/SQL will not be applied.

    using (var scope = Current.ScopeProvider.CreateScope())
    {
        var sql = scope.SqlContext.Sql()
            .Select<Record>(x=>x.Form)
            .From<Record>()
            .Where<Record>(x => x.UniqueId == recordId);
    
        var formId = scope.Database.ExecuteScalar<Guid>(sql);
    
        scope.Complete();
    
        return GetForm(formId);
    }
    
  • Tim Geyssens 6419 posts 15026 karma points MVP 2x c-trib
    Jan 03, 2019 @ 00:43
    Tim Geyssens
    0

    Hi Warren, thanks for the details, well I need it to run more then once since I'm building up types on the fly based user input.... , is there a way I can force execute a migration? Or in your second example how would you create a table based on a type?

  • Warren Buckley 2085 posts 4501 karma points MVP 6x admin hq c-trib
    Jan 03, 2019 @ 19:32
    Warren Buckley
    0

    I would use the Scope approach in that case as you need to do it at runtime it seems so you could then create your new table as needed I assume of some GUI or some user based input/action

  • Tim Geyssens 6419 posts 15026 karma points MVP 2x c-trib
    Jan 04, 2019 @ 08:23
    Tim Geyssens
    0

    THanks, got an example of using the Scope approach and combining it with

    if (!TableInfo.FromPoco(typeof(BlogComment)).TableName)
    {
        Create.Table<BlogComment>().Do();
    }
    

    Since I don't see any create table stuff on there

  • Bo Jacobsen 256 posts 1195 karma points
    Mar 07, 2019 @ 16:22
    Bo Jacobsen
    0

    Hi Tim.

    Did you ever find a way to create tables without migration?

  • Marcio Goularte 339 posts 1137 karma points
    Mar 07, 2019 @ 18:21
  • Bo Jacobsen 256 posts 1195 karma points
    Mar 07, 2019 @ 18:27
    Bo Jacobsen
    0

    Hi Marcio.

    It's the same as the suggestions in this thread. But it still uses Migration. And i wanted to know if it was possible without Migration.

  • Tim Geyssens 6419 posts 15026 karma points MVP 2x c-trib
    Mar 08, 2019 @ 08:14
    Tim Geyssens
    0

    Nope not yet Bo, @Warren any further ideas?

Please Sign in or register to post replies

Write your reply to:

Draft