Skip to content

BulkCopy ignores IDataContext.CloseAfterUse option for SQL Server #4257

Description

@Usaga

Hi!

I found that BulkCopy(Async) ignores IDataContext.CloseAfterUse option for at least SQL Server. We are mixing Entity Framework and Linq2Db for time of migration from EF. And we want to have same behavior of managing connections as EF does (close right after query completed).

This works fine, but seem BulkCopy ignores this option that leads to distributed transaction, which is very unwanted.

Steps to reproduce

        public class Test
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        internal class Context : DataConnection
        {
            public const string ConnString = "...connection string...";

            static Context()
            {
                SqlServerTools.Provider = SqlServerProvider.SystemDataSqlClient;
            }

            public Context() : base(LinqToDB.ProviderName.SqlServer2012, ConnString)
            {
                (this as IDataContext).CloseAfterUse = true; // Very vital for us
            }
        }

            using var scope = new TransactionScope();

            using var ctx = new Context();

            var t = new List<Test>() { new Test { Id = 55, Name = "test" }, new Test { Id = 355, Name = "ewrwe" } };

            ctx.BulkCopy(t);
            //ctx.Close(); // this is workaround
            var conn = new SqlConnection(Context.ConnString);

            conn.Open(); // got error here

            var cmd = new SqlCommand("select 1", conn);
            cmd.ExecuteNonQuery();

Environment details

Linq To DB version: 5.2.2

Database (with version): SQL Server 20**

ADO.NET Provider (with version): SystemDataSqlClient

Operating system: Windows 10

.NET Version: 7.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status: has-prThere is active PR for issue

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions