I need to use transactions with a ADO.NET provider.
Below is a simple example of a connection, transaction and command being created. When I create a command using connection.CreateCommand()
do I need to assign the transaction to the command? Or, is the transaction set because I'm using connection.CreateCommand()
vs newing up a command object?
var connection = Database.GetConnection();
connection.Open();
var transaction = connection.BeginTransaction();
var command = connection.CreateCommand();
command.Transaction = transaction; // Is this line needed when using connection.CreateCommand()?
*Update*
When I test the reference of both objects, they are the same. I'd assume that means connection.CreateCommand()
is returning a command with the transaction assigned. Or maybe that is not a valid test.
using (var connection = Database.GetConnection())
{
connection.Open();
var transaction = connection.BeginTransaction();
var command = connection.CreateCommand();
if (object.ReferenceEquals(transaction, command.Transaction))
Debug.WriteLine("EQUAL");
}