Copied to clipboard

Flag this post as spam?

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


  • Philip Hayton 98 posts 435 karma points
    Dec 11, 2018 @ 12:28
    Philip Hayton
    0

    Umbraco Persistence Transactions

    Hola amigos,

    I'm using the Umbraco Persistence library to store data in custom tables within the Umbraco database.

    I've built a service layer to handle the business logic during CRUD operations. For one such operation I need to wrap several commands inside a transaction.

    However, when I call the UmbracoDatabase.BeginTransaction() method, it throws a System.NullReferenceException error. Having inspected the code, I think it's the connection that is null.

    It's weird though because I am passing in an existing UmbracoDatabase from an UmbracoAuthorizedJsonController: DatabaseContext.Database. I've also tried calling OpenSharedConnection() - to no avail.

    I've tried to identify the source code on GitHub but I can't actually find the 'UmbracoDatabase' class anywhere, nor can I find any documentation.

    Is anyone able to shed any light? Am I being silly?

    Any help is greatly appreciated!

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Dec 11, 2018 @ 13:31
    Dave Woestenborghs
    0

    Hi Philip,

    Welcome to our.

    I think you need to use the following code :

     using (var transaction = DatabaseContext.Database.GetTransaction())
                {
                   // do your stuff here
    
                    transaction.Complete();
                }
    

    Dave

  • Philip Hayton 98 posts 435 karma points
    Dec 12, 2018 @ 09:15
    Philip Hayton
    0

    Hi Dave,

    Thanks for the suggestion! :)

    Unfortunately it's still throwing the same null exception. It's odd because if I remove the transaction code, calls to the DB work as expected. It's as if the connection / transaction is created at SQL run-time, for example when executing

    Db.SingleOrDefault()

    or

    Db.Page()

    It's good to know that method exists though, I had missed that earlier.

    Cheers

    Phil

Please Sign in or register to post replies

Write your reply to:

Draft