Control Dapper Transaction with Simplecrud / Error: Enlisting in Ambient transactions is not supported

Asked

Viewed 204 times

0

Good morning guys, all right?

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

I will post parts of the code only (removing conversions from Viewmodel to Model and validations) not to be so complex.

but in short, I have the Service>Business>Repository>Base

Service makes the integration of Business when it is necessary the integration between several Business.

In 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);
    }

Na Repository

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

    return entity;
}

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

Scope apparently cannot control transactions within Simplecrud.

Someone could help me in this case, I’ve seen examples working, but there was no separation of layers.

In the case of changing Simplecrud, I have the situation of annotating where some columns are not changed in Update, and Simplecrud has the "Dapper.Ignoreupdate" annotation that helps me in this case.

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

thank you

1 answer

0

Simplecrud accepts the transaction as a parameter in CUD methods, but it comes from _dbConnection, to control this you will need to implement the Unitofwork Pattern, it will centralize the connections and repositories.

Follow the example code on github:

Dapperunitofwork

In the example the repository implements the direct Dapper, but you can keep the Simplecrud, passing only the transaction in the methods that need transaction.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.