Page 1 of 1

EFCore cross context transaction support

Posted: Tue 20 Aug 2019 12:58
by ggercman
Hi,
I'm trying to create transaction and use it for cross database context as in this article https://docs.microsoft.com/en-us/ef/cor ... ansactions.
On line t.Commit() an exception is beeing thrown. After inspecting, I think it is because connection is beeing closed after disposing db2. db2 does not own connection, so this behavior is incorrect.
Comparing other provider (sqlserver) con.State is Open after disposing db2.

Code: Select all

using (var con = new Devart.Data.Oracle.OracleConnection(connection))
            {
                con.Open();
                var options1 = new DbContextOptionsBuilder<Db1>();
                options1.UseOracle(con);

                using (var db1 = new Db1(options1.Options))
                {
                    await db1.SaveChangesAsync();
                    using (var t = await db1.Database.BeginTransactionAsync())
                    {

                        var options2 = new DbContextOptionsBuilder<Db2>();
                        //options2.UseOracle((db1.Database.GetDbConnection() as Devart.Data.Oracle.Entity.IOracleConnectionAdapter).Connection);
                        options2.UseOracle(con);

                        using (var db2 = new Db2(options2.Options))
                        {
                            db2.Database.UseTransaction(db1.Database.CurrentTransaction.GetDbTransaction());
                            await db2.SaveChangesAsync();
                        }

                        await db1.SaveChangesAsync();
                        t.Commit();
                    }
                }
            }
            
            

    public class A1
    {
        public int Id { get; set; }
    }
    public class A2
    {
        public int Id { get; set; }
    }
    public class Db1 : DbContext
    {
        public Db1(DbContextOptions<Db1> options) : base(options) { }
        public DbSet<A1> A1 { get; set; }
    }
    public class Db2 : DbContext
    {
        public Db2(DbContextOptions<Db2> options) : base(options) { }
        public DbSet<A2> A1 { get; set; }
    }            

Re: EFCore cross context transaction support

Posted: Fri 23 Aug 2019 17:46
by Shalex
Thank you for your report. We have reproduced the issue with throwing "This OracleTransaction has completed; it is no longer usable." and are investigating it. We will notify you about the result.

Re: EFCore cross context transaction support

Posted: Sat 14 Sep 2019 11:20
by Shalex
The bug with using the same database connection in several EF Core contexts is fixed: viewtopic.php?f=1&t=39337.