Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
How can I get the database in v8?
In v7 I can find it in ApplicationContext.Current.DatabaseContext.Database
From a conversation here: https://github.com/umbraco/UmbracoDocs/issues/745
I'm deducing the Database is now available via a scope object?
using (var scope = ApplicationContext.Current.ScopeProvider.CreateScope())
var database = scope.Database;
// use database
but is a bit of a guess.
Thanks Marc, that seems to work :)
Also used this var db = new Database("umbracoDbDsn"); but I guess a magic string is bad practice, and maybe also storing the database in a variable?
var db = new Database("umbracoDbDsn");
It just feels cumbersome to do all that using and Complete() stuff whenever I need something from the database.
using and Complete() stuff
Do read this comment from Stephane,
The "cumbersome" using and Complete() stuff is there for safety, mostly for when database updates fail and need to be rolled back.
For querying, use IUmbracoDatabaseFactory and make sure to dispose after usage so you don't keep connections to the db open. I don't know if NPoco's Database class doesn't dispose either, so you will in all cases need that using stuff. :)
Kevin jump answered this to me in this post
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted