Reputation: 46
How can I insert a record a their childs in a single transaction?
For example I have a class named Requisition
(IdRequisition
, Description
, RegisterDate
) and RequisitionRequirement
(IdRequisitionRequirement
, IdRequisition
, DocumentName
, DeliveryDate
, LimitDate
) and I need to insert the Requisition
and get the IdRequisition
by outparameter and then insert the list of RequisitionRequirements
that must being related with the Requisition
, in a single transaction and ensure that if one of the RequisitionRequirement
fails the insert, do a rollback.
I don't know what is the correct form to do it, if handling transactions from c# with cmd.Connection.BeginTransaction()
or another way.
I was using Linq to entities and we had no problem with that, but the current app uses Oracle and stored procedures and I'am new in this context.
Sorry for my english but I'm learning too !
Upvotes: 0
Views: 1562
Reputation: 11094
Just wrap your code in a transaction scope. Do not close the connection while calling the stored procs inside the scope. BTW consider using a function to return the IdRequisition instead of using an out parameter.
using (TransactionScope scope = new TransactionScope())
{
// Save Requisition and get IdRequisition
// Save RequisitionRequirements
}
Upvotes: 1
Reputation: 94653
First of all you have to open the connection and then retrieve transaction instance by calling connection.BeginTransaction()
method.
Take a look at ODP.NET article - Choose the ODP.NET transaction type that fits your business rules.
Upvotes: 0