Copied to clipboard

Flag this post as spam?

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


  • Søren Kottal 530 posts 3521 karma points MVP 2x c-trib
    Dec 28, 2018 @ 10:40
    Søren Kottal
    0

    Getting the Database in v8

    How can I get the database in v8?

    In v7 I can find it in ApplicationContext.Current.DatabaseContext.Database

  • Marc Goodson 1451 posts 9716 karma points MVP 5x c-trib
    Dec 29, 2018 @ 19:16
    Marc Goodson
    101

    Hi Soren

    From a conversation here: https://github.com/umbraco/UmbracoDocs/issues/745

    I'm deducing the Database is now available via a scope object?

    eg

    using (var scope = ApplicationContext.Current.ScopeProvider.CreateScope())
    {
      var database = scope.Database;  
      // use database  
      scope.Complete();
    }
    

    ??

    but is a bit of a guess.

    regards

    marc

  • Søren Kottal 530 posts 3521 karma points MVP 2x c-trib
    Dec 31, 2018 @ 09:27
    Søren Kottal
    0

    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?

    It just feels cumbersome to do all that using and Complete() stuff whenever I need something from the database.

  • Sebastiaan Janssen 4899 posts 14655 karma points MVP admin hq
    Feb 28, 2019 @ 11:03
    Sebastiaan Janssen
    1

    Do read this comment from Stephane,

    https://github.com/umbraco/Umbraco-CMS/issues/3866#issuecomment-466907932

    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. :)

  • Marcio Goularte 356 posts 1248 karma points
    Feb 28, 2019 @ 01:37
  • 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