Controlling Dapper transaction with SimpleCrud / Error: Enlisting in Ambient transactions is not supported

0

Good morning everyone, alright?

I need to control the Dapper transaction using SimpleCrud. I have an application in asp.net core using Injection Dependency.

I'll post parts of the code only (removing ViewModel to Model conversions and validations) so it does not get so complex.

But in summary, I have the Service > Business & Repository & Base

Service integrates Business when integration between multiple Business is required.

Na service

public async Task SaveAsync(CourseViewModel courseViewModel)
        {
            using (var transaction = new TransactionScope())
            {
                await this._courseBusiness.SaveAsync(courseViewModel);
                transaction.Complete();
            }


            }
    }

In Business

    public async Task SaveAsync(CourseClassViewModel courseClassViewModel)
    {
           await this._courseClassRepository.InsertAsync(courseClassViewModel);
    }

In the Repository

public async Task<T> InsertOnDataBaseAsync(T entity)
{
    Tkey id =  await _dbConnection.InsertAsync<Tkey,T>(entity);
    entity.Id = id;            

    return entity;
}

When I do the control with Scope, I get the following error message "Enlisting in Ambient transactions is not supported"

Apparently Scope can not handle transactions within SimpleCrud.

Someone could help me in this case, I've seen examples working, but there was no layer separation.

In the case of changing SimpleCrud, I have the situation of making annotations where some columns are not changed in Update, and SimpleCrud has the annotation "Dapper.IgnoreUpdate" that helps me in that case.

I think that's it, if anyone can help me.

Thank you

    
asked by anonymous 01.11.2018 / 14:29

1 answer

0

SimpleCrud accepts transaction as a parameter in CUD methods, however it comes from _dbConnection, to control this you will need to implement UnitOfWork Pattern, it will centralize connections and repositories.

Here is the sample code in github:

DapperUnitOfWork

In the example the repository implements the direct dapper, however you can keep SimpleCrud, passing only the most transaction to the methods that need to be transacted.

    
31.12.2018 / 14:41