Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Dapper and Entity Framework within the same transaction

Are there any issues/gotchas using Dapper and Entity Framework 6 in the same database transaction with SQL Server?

like image 956
burnt1ce Avatar asked Jun 16 '26 04:06

burnt1ce


1 Answers

using (var db = new MyDbContext())
using (var tran = db.Database.BeginTransaction()){

    //EF
    var hoge = db.HogeTable.FirstOrDefault();
    hoge.Name = "fuga";
    db.SaveChanges();

    //Dapper
    // create query and params
    db.Database.Connection.Execute(query, params, tran.UnderlyingTransaction);
    tran.Commit();
}
like image 162
kinpo Avatar answered Jun 17 '26 19:06

kinpo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!