Open the SqlConnection and set XACT_ABORT ON before passing the open SqlConnection to your DbContext constructor. The pattern for BYO-connection in EF core is
SqlConnection con;
public Db(SqlConnection con)
{
this.con = con;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(loggerFactory)
.UseSqlServer(con, o => o.UseRelationalNulls());
base.OnConfiguring(optionsBuilder);
}
public override void Dispose()
{
con.Close();
base.Dispose();
}
or if you're using DI, introduce a IDbConnectionFactory or somesuch and write
public Db(IDbConnectionfactory cf)
{
this.con = cf.GetConnection();
}
Passing an open connection to the DbContext constructor will prevent the DbContext from opening and closing the connection for each command. This might slightly increase the size of your connection pool, but it shouldn't be a big deal.