Efcore: how to perform an EF Core operation and an ADO.NET SqlClient operation in the same transaction,Using db.Database.BeginTransaction

Created on 10 Nov 2017  路  3Comments  路  Source: dotnet/efcore

```C#
public void Mian()
{
using (var transaction = db.Database.BeginTransaction())
{try
{
var aa = db.Bookes.ToList();
db.SqlQuery(param);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}
}

public static IList SqlQuery(this DbContext db, CommandType type, string sql, List parameters) where T : new()
{
var conn = db.Database.GetDbConnection();
try
{
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
using (var command = conn.CreateCommand())
{
using (var reader = command.ExecuteReader())
{
}
}
}
```

How can i using transaction in SqlQuery?
using :EF Core 1.0+VS2015

thanks,

closed-question

Most helpful comment

@zolf1983 After creating the command it needs to have the transaction set. For example:
C# command.Transaction = db.Database.CurrentTransaction.GetDbTransaction();

All 3 comments

@zolf1983 Can you be more specific about what you mean by "using transaction"? In general, if EF begins a transaction with BeginTransaction, then any commands executed on that connection can participate in that transaction.

@ajcvickers

```C#
public IActionResult Index()
{
using (var transaction = db.Database.BeginTransaction())
{
try
{
var list = db.Users.ToList();
var list2 = db.SqlQuery(CommandType.Text,
" SELECT top 100 UserName,TrueName FROM [dbo].[AspNetUsers]", null).ToList();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}
return View();
}

public static IList SqlQuery(this DbContext db, CommandType type, string sql, List parameters) where T : new()
{
var conn = db.Database.GetDbConnection();
try
{
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
using (var command = conn.CreateCommand())
{
command.CommandText = sql;
command.CommandType = type;
if (parameters != null && parameters.Count() > 0)
{
foreach (var item in parameters)
{
DbParameter p = command.CreateParameter();
p.DbType = item.DbType;
p.ParameterName = item.ParameterName;
p.Value = item.Value;
command.Parameters.Add(p);
}
}
var propts = typeof(T).GetProperties();
var rtnList = new List();
T model;
object val;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
model = new T();
foreach (var l in propts)
{
val = reader[l.Name];
if (val == DBNull.Value)
l.SetValue(model, null);
else
l.SetValue(model, val);
}
rtnList.Add(model);
}
}
return rtnList;
}
}
catch (Exception ex)
{
return null;
}
}

        public class ResultDTO
        {
            public string UserName { get; set; }
            public string TrueName { get; set; }
        }

```

In the main method, Begin EF core Transaction. I using this dbcontext Connection CreateCommand in SqlQuery. Using this ADO.NET Operation database.

The program will be errors at command.ExecuteReader() . ExecuteReader requires commands to have transactions锛孴ransaction property have not Initialization of this command. have can i get db.transaction using command? using ef core 1.0
thanks.

@zolf1983 After creating the command it needs to have the transaction set. For example:
C# command.Transaction = db.Database.CurrentTransaction.GetDbTransaction();

Was this page helpful?
0 / 5 - 0 ratings