The transaction between Dapper and EF Core fails with an error. Parallel transactions are not supported by SqlConnection.

.net-core c# dapper entity-framework entity-framework-core

Question

And I tried to utilize Dapper and EF Core together inside of a TransactionScope when I called context, I first began utilizing EF Core. I have a SaveChanges() error.SqlConnection does not support parallel transactions. The code reads as follows:

public class TestController : ControllerBase
{
    private readonly MyDbContext dbContext;
    private readonly IDbConnection dbConnection;

    public TestController(MyDbContext dbContext,
        IDbConnection dbConnection)
    {
        this.dbContext = dbContext;
        this.dbConnection = dbConnection;
    }

    public void Test2()
    {
        using (IDbTransaction transaction = dbConnection.BeginTransaction())
        {
            Client client = new Client();
            client.ClientId = 3;
            client.Name = "New Client 3";

            dbContext.Entry<Client>(client).State = EntityState.Modified;

            List<Client> clients = new List<Client>();
            clients.Add(new Client
            {
                ClientId = 1,
                Name = "New Client 1",
            });
            clients.Add(new Client
            {
                ClientId = 2,
                Name = "New Client 2",
            });
            clients.Add(new Client
            {
                ClientId = 4,
                Name = "New Client 4",
            });

            string sql = "UPDATE Client SET Name = @Name WHERE ClientId = @ClientId;";

            try
            {
                dbConnection.Execute(sql, clients, transaction: transaction);

                dbContext.SaveChanges();

                transaction.Commit();
            }
            catch (System.Exception ex)
            {

            }
        }
    }
}

How can I properly blend EF Core and Dapper activities in a single transactionscope?

1
0
12/19/2019 7:29:41 AM

Popular Answer

Anyone experiencing a similar issue may follow what I did to find a solution.

public class TestController : ControllerBase
{
    private readonly MyDbContext dbContext;
    private readonly DbConnection dbConnection;

    public TestController(MyDbContext dbContext,
        DbConnection dbConnection)
    {
        this.dbContext = dbContext;
        this.dbConnection = dbConnection;
    }

    public void Test2()
    {
        IExecutionStrategy strategy = dbContext.Database.CreateExecutionStrategy();
        strategy.Execute(() =>
        {
            using (DbTransaction transaction = dbConnection.BeginTransaction())
            {
                Client client = new Client();
                client.ClientId = 3;
                client.Name = "New Client 3";

                dbContext.Database.UseTransaction(transaction);

                dbContext.Entry<Client>(client).State = EntityState.Modified;

                List<Client> clients = new List<Client>();
                clients.Add(new Client
                {
                    ClientId = 1,
                    Name = "New Client 1",
                });
                clients.Add(new Client
                {
                    ClientId = 2,
                    Name = "New Client 2",
                });
                clients.Add(new Client
                {
                    ClientId = 4,
                    Name = "New Client 4",
                });

                string sql = "UPDATE Client SET Name = @Name WHERE ClientId = @ClientId;";

                try
                {
                    dbContext.SaveChanges();

                    dbConnection.Execute(sql, clients, transaction: transaction);

                    transaction.Commit();
                }
                catch (System.Exception ex)
                {

                }
            }
        });
    }
}
1
12/19/2019 8:19:00 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow