Copied to clipboard

Flag this post as spam?

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


  • Josip 195 posts 662 karma points c-trib
    Aug 25, 2019 @ 18:34
    Josip
    0

    Does anyone have an example of how to create a table using npoco? I read this blog post about peta poco and Umbraco 7, and i dont know how to write this part in Umbraco 8 and NPoco:

        namespace MyWebsite
    {
        public class RegisterEvents : ApplicationEventHandler
        {
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                //Get the Umbraco Database context
                var db = applicationContext.DatabaseContext.Database;
    
    
                if (!db.TableExist("BlogComments"))
                {
    
                    db.CreateTable<BlogComment>(false);
                }
    
                Document.BeforePublish += Document_BeforePublish;
            }
    
            private void Document_BeforePublish(Document sender, PublishEventArgs e)
            {
    
                Log.Add(LogTypes.Debug, sender.Id, "the document " + sender.Text + " is about to be published");
    
                e.Cancel = true;
            }
        }
    }
    
  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Aug 25, 2019 @ 20:31
    Marc Goodson
    100

    Hi Josip

    There's a great resource in this github repository about 'how to do stuff with Umbraco' written by Umbraco legend Kevin Jump, which are mainly his notes for remembering how to do things when he creates his packages and site implementations...

    https://github.com/KevinJump/DoStuffWithUmbraco

    ... I mention it because he has an example for V8 of how to create a database table using NPoco

    https://github.com/KevinJump/DoStuffWithUmbraco/blob/master/Src/DoStuff.Core/Migrations/CreateTable/CreateTableMigration.cs

    inside 'a migration'

    Migrations run once so are ideal for creating things like tables...

    Also there is an example here in the docs:

    https://our.umbraco.com/Documentation/Extending/Database/

    regards

    Marc

  • Josip 195 posts 662 karma points c-trib
    Aug 26, 2019 @ 09:08
    Josip
    0

    Hello Marc,

    Thank you for this valuable informations, I spent 2 days looking for something like that :)

    I tried to copy - paste example from Umbraco docs and I am getting red underline from intellisense : enter image description here

    Is that mistake, should it be called BlogCommentSchema ?

    BR

    Josip

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Aug 26, 2019 @ 11:30
    Marc Goodson
    0

    Hi Josip

    I reckon you are correct!

    as that does appear to be the class which defines the table.

    does it work as expected when you use BlogCommentSchema ?

    if so, it would be ace if you could edit the documentation page, and create a PR to the repo to correct it.

    regards

    marc

  • Josip 195 posts 662 karma points c-trib
    Aug 26, 2019 @ 11:43
    Josip
    0

    Yes it creates the table when i change to "BlogCommentSchema".

    it would be ace if you could edit the documentation page, and create a PR to the repo to correct it.

    Sure i will :)

    P.S. i have small problems when i want to insert data to the table, but I created another topic for that here

    BR

    Josip

Please Sign in or register to post replies

Write your reply to:

Draft