1

Topic: DbConnection. CreateCommand and local transaction

I want to consult. Whether should DbConnection. CreateCommand to anchor a formed team to the active local transaction? I here look at behavior System. Data. OleDb - does not anchor. And if to try request through such command - the error is generated. {var cn=new xdb. OleDbConnection (TestServices. Conf __ GetCnStr ()); cn. Open (); using (var tr=cn. BeginTransaction ()) {var cmd=cn. CreateCommand (); Assert. That (cn, Is. Not. Null); Assert. That (cmd. Connection, Is. SameAs (cn)); Assert. IsNull (cmd. Transaction);//DOES NOT ANCHOR cmd. CommandText = "select ID from DUAL"; var rd=cmd. ExecuteReader ();//THROWS the EXCEPTION rd. Close ();} //using tr} System. InvalidOperationException: ExecuteReader demands, that the command had transaction if the connection, assigned to a command, is in the postponed local transaction Property Transaction for a command is not initialized. In my opinion this illogical behavior. Because, if somewhere transferred object of connection for operation performance there should not be soared concerning that there is a local transaction or it is not present. As DbConnection does not give possibility to define presence of local transaction - at it is not present property of type CurrentTransaction. As a matter of fact, I ran into this problem, transferring connection with the active local transaction inside EntityFrameworkCore - it there  DbConnection. CreateCommand and (the clear stub) does not anchor the received command to this local transaction.

2

Re: DbConnection. CreateCommand and local transaction

Hello, Kovalenko Dmitry, you wrote: Database. BeginTransaction

3

Re: DbConnection. CreateCommand and local transaction

Hello, vorona, you wrote: V> Hello, Kovalenko Dmitry, you wrote: V> Database. BeginTransaction That that at EFCore is transaction management, it is clear. It is a question about the type scenario: using (DbConnection cn=new xdb. OleDbConnection (TestServices. Conf __ GetCnStr ())) {cn. Open (); using (var tr=cn. BeginTransaction ()) {using (var db=new MyContext (cn))//we transfer ready connection with transaction {foreach (var rec in db.testTable)//the error {here gets out

4

Re: DbConnection. CreateCommand and local transaction

Hello, Kovalenko Dmitry, you wrote: TransactionScope

5

Re: DbConnection. CreateCommand and local transaction

> I Want to consult. Whether > Should DbConnection. CreateCommand to anchor a formed team to the active local transaction? If a command is executed on a connection with an active transaction, and the Transaction object has not been assigned to the Transaction property of the Command object, an exception is thrown. You can initiate a local transaction with the BeginTransaction method. Once you have begun a transaction, you can enlist a command in that transaction with the Transaction property of a Command object. You can then commit or roll back modifications made at the data source based on the success or failure of the components of the transaction. using (SqlConnection connection = new SqlConnection (connectionString)) {connection. Open ();//Start a local transaction. SqlTransaction sqlTran = connection. BeginTransaction ();//Enlist a command in the current transaction. SqlCommand command = connection. CreateCommand (); command. Transaction = sqlTran;

6

Re: DbConnection. CreateCommand and local transaction

Hello, takTak, you wrote: >> I Want to consult. Whether >> Should DbConnection. CreateCommand to anchor a formed team to the active local transaction? T> If a command is executed on a connection with an active transaction, and the Transaction object has not been assigned to the Transaction property of the Command object, an exception is thrown. It is clear. The question was about another - whether should DbConnection. CreateCommand independently to anchor a formed team to local transaction or should not? We admit at you there is object DbConnection and more than anything. Whether to learn there is at it the active transaction or not - it does not turn out. Because DbConnection does not give such possibility. But gives CreateCommand and anchors returned object of a command to itself. But does not anchor to local transaction. As a result of CreateCommand returns the left unfinished object, and to complete it it is not obviously possible. Because there is no access to local transaction. Here I also think - DbCommand. CreateCommand  should install itself command. Transaction if connection has a local transaction. - - I want to make it in the ADO.NET the provider. I will eliminate one more illogicality which hinders to write "rather interesting programs"

7

Re: DbConnection. CreateCommand and local transaction

Whether >>> Should DbConnection. CreateCommand to anchor a formed team to the active local transaction? I to you in English resulted the link: if transaction is opened also you of it do not do, you will receive an exception, try to ask Transaction. Current as it does ADO.NET automatically at any Connection. Open it is simple not the fact that is necessary everywhere any transactions behind itself to drag: if you simply read, it is only additional lock which will decelerate reading

8

Re: DbConnection. CreateCommand and local transaction

Hello, takTak, you wrote: whether >>>> Should DbConnection. CreateCommand to anchor a formed team to the active local transaction? T> I to you in English resulted the link: if transaction is opened also you of it do not do, you will receive an exception, T> try to ask Transaction. Current as it does ADO.NET automatically at any Connection. Open I all looked at it. Now my provider himself throws an exception. A problem in other. DbConnection. CreateCommand forms the left unfinished object of a team which cannot use without additional . T> it is simple not the fact that is necessary everywhere any transactions behind itself to drag: if you simply read, it is only additional lock which will decelerate reading Once again. I transfer in unknown service (in this case it EFCore) object DbConnection. This service knows nothing about local transaction and simply causes connection. CreateCommand for performance of the operation. In a current variant it cannot fulfill its (operation) - because is not installed command. Transaction. If connection. CreateCommand installs cmd. Transaction all normally functions. - - Or services should work not with DbConnection, and with DbTransaction. Through DbTranaction. Connection. CreateCommand to form a team and to anchor it to transaction. But EFCore works with exterior DbConnection.

9

Re: DbConnection. CreateCommand and local transaction

> Or services should work not with DbConnection, and with DbTransaction. Through DbTranaction. Connection. CreateCommand to form a team and to anchor it to transaction. > But EFCore works with exterior DbConnection. I do not understand something,   connection or transaction any there? For what you do it? You could transfer connectionString, a that services create that is necessary to them if speech about distributed transaction and then to invent a bicycle it is not necessary, to distributed transaction it is possible to join, if MSDTC is activated at all these dances with a tambourine you still should to consider such scenarios that you in command which is fulfilled, have the code, which itself opens in t-sql the transaction and in a case rolback  to work successfully

10

Re: DbConnection. CreateCommand and local transaction

11

Re: DbConnection. CreateCommand and local transaction

> I like to cross  to a hedgehog. In my opinion, this keyword in all your history as conducts itself connection pool, what speed of performance etc., etc. , you need to get with ears into the code of already existing providers to understand all corners and problems

12

Re: DbConnection. CreateCommand and local transaction

Hello, takTak, you wrote: >> I like to cross  to a hedgehog. T> in my opinion, this keyword in all your history T> as itself(himself) It conducts connection pool, itself(himself) there as does not conduct. Because it will not be involved. T> what speed of performance etc., etc. Above than if to transfer a line of connection and inside to start "automatic" transactions for each operation from a DB (if DbConnection without local transaction). T> , you need to get with ears into the code of already existing providers to understand all corners and problems I therefrom I do not get out the last 10 years. Simply there is an official scenario, it does not work. The reason is clear. It also I try to consider.