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();
}
}
}
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
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?
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
Topic author was deleted
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 you use migrations, you could use something like this:
Stephan has write a blog about migrations in V8 - https://www.zpqrtbnk.net/posts/migrations-in-v8
Comment author was deleted
thanks, will give it a go!
Hi Jan
I think you need to add
.Do()
to yourCreate.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:
That is how I got it working :)
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.Comment author was deleted
Thanks for the extra info Søren
Comment author was deleted
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/
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
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.Comment author was deleted
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?
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
Comment author was deleted
THanks, got an example of using the Scope approach and combining it with
Since I don't see any create table stuff on there
Hi Tim.
Did you ever find a way to create tables without migration?
in this post they replied to me how to make the custom table
https://our.umbraco.com/forum/umbraco-8/95939-create-custom-tables-migration-umbracodatabase-npoco
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.
Comment author was deleted
Nope not yet Bo, @Warren any further ideas?
is working on a reply...