Search code examples
c#sqlentity-frameworkentity-framework-coreaspnetboilerplate

ASP.NET Boilerplate Entity commit immediately


I've started working with Entities within ASP.NET Boilerplate. Im trying to get my entities to commit to the database immediately but i cant seem to do it.

I am calling await CreateAsync(EntityDto);.

I've tried overriding the CreateAsync within my controller to include CurrentUnitOfWork.SaveChanges();.

I have also tried to wrap the code in a unit of work.

        using (var unitOfWork = _unitOfWorkManager.Begin())
        {
            var entityDto = await CreateAsync(entityDto);
            await unitOfWork.CompleteAsync();
        }

But it never seems to commit to the database immediately.

Any help would be much apricated.


Solution

  • ABP's Conventional Unit of Work is scoped around your Controller action and The unit of work is ambient.

    You should require a new unit of work with TransactionScopeOption.RequiresNew:

    using (var unitOfWork = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
    {
        var entityDto = await CreateAsync(entityDto);
        await unitOfWork.CompleteAsync();
    }
    

    Alternatively, you can suppress the unit of work with TransactionScopeOption.Suppress:

    using (var unitOfWork = _unitOfWorkManager.Begin(TransactionScopeOption.Suppress))
    {
        var entityDto = await CreateAsync(entityDto);
        await unitOfWork.CompleteAsync();
    }