How to use entity framework transaction in raw query?

Viewed 662

I am using entity framework but doing my operations with raw queries. My operations are like following:

  1. Check if recırd exist with integration_id
  2. Delete record if exit
  3. Insert new record

So I am using transaction

using (var transaction = await _context.Database.BeginTransactionAsync())
{ 
        var isExist = await IsExist(id);

        if (isExist)
        {
              var deleteQuery = "delete from ....";
              await _context.Database.ExecuteSqlRawAsync(deleteQuery);
        }

        var insertQuery = "insert into ...";
        await _context.Database.ExecuteSqlRawAsync(insertQuery);
}

if insert operation fails, does deleted record rollback?

1 Answers

This method doesn't start transaction on it's own. If you need to execute queries in transaction you need to first call

BeginTransaction(DatabaseFacade, IsolationLevel) or UseTransaction.

Reference docs.microsoft.com

So in your case it will execute queries in a transaction and roll back all the queries if any of the query failed

Related