Copied to clipboard

Flag this post as spam?

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


  • Alex Brown 129 posts 620 karma points
    Nov 16, 2017 @ 16:19
    Alex Brown
    0

    Database.GetTransaction Throws Null Exception

    Hi All

    I'm using Umbraco 7.7.6, I've recently upgraded from 7.5.13.

    I've created a class called UnitOfWork which is used as a container for the instance of Database. This all seemed to work on 7.5.13 but doesn't seem to like creating a new transaction on the latest version.

    Calling the database works, it just throws a null exception when creating a transaction.

    Code as follows.

    DefaultRegistry (I'm using StructureMap):

        For<IUnitOfWork>().Use<UnitOfWork>()
            .Ctor<Database>().Is(() => ApplicationContext.Current.DatabaseContext.Database);
    

    UnitOfWork.cs:

    private readonly Database _db;
    public UnitOfWork(Database db)
    {
        _db = db;
    }
    
    public Transaction GetTransaction()
    {
        return _db.GetTransaction();
    }
    

    Error:

       at Umbraco.Core.Persistence.Database.BeginTransaction(IsolationLevel isolationLevel)
       at Umbraco.Core.Persistence.Database.GetTransaction()
       at CMS.Library.DataAccess.UnitOfWork.GetTransaction() in \\CMS.Library\DataAccess\Implementation\UnitOfWork.cs:line 330
       at CMS.Library.Services.ProductService.Save(ProductViewModel productViewModel) in \\Services\ProductService.cs:line 254
    

    Thanks

  • Alex Brown 129 posts 620 karma points
    Nov 17, 2017 @ 08:56
    Alex Brown
    0

    Okay so I fixed it, but I feel like it's wrong. Here's the registry:

    For<Database>().Use(() => new Database("umbracoDbDSN")).Transient();
    
    For<IUnitOfWork>().Use<UnitOfWork>()
        .Ctor<Database>().Is(() => new Database("umbracoDbDSN"));
    
  • 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