Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you include linq-to-sql changes and ADO.NET dataset table adapter updates in a single transaction?
    primarykey
    data
    text
    <p>Here are the relevant technologies that I'm working with:</p> <ul> <li>Devart's dot Connect for Oracle (to facilitate Linq-to-Sql for Oracle).</li> <li>Strongly Typed ADO.NET Datasets.</li> <li>An Oracle database.</li> </ul> <p>Here's the challenge:</p> <ul> <li>My legacy code submits database updates with ADO.NET datasets and table adapters.</li> <li>I'd like to begin converting that code over to Linq-to-Sql, but I'd like to do it piecemeal to minimize code churn and risk.</li> </ul> <hr> <p>Here's my proof of concept schema:</p> <p>Parent Table</p> <ul> <li>Parent.Id</li> <li>Parent.Name</li> </ul> <p>Child Table</p> <ul> <li>Child.Id</li> <li>Child.ParentId</li> <li>Child.Name</li> </ul> <p>Here's my proof of concept code block:</p> <pre><code>using System; using System.Data.Common; using DevArtTry1.DataSet1TableAdapters; namespace DevArtTry1 { class Program { static void Main(string[] args) { using (DataContext1 dc = new DataContext1()) { dc.Connection.Open(); using (DbTransaction transaction = dc.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)) { dc.Transaction = transaction; Parent parent = new Parent(); parent.Id = 1; parent.Name = "Parent 1"; dc.Parents.InsertOnSubmit(parent); dc.SubmitChanges(); // By virtue of the Parent.Id -&gt; Child.ParentId (M:N) foreign key, this statement will impose a write lock on the child table. DataSet1.CHILDDataTable dt = new DataSet1.CHILDDataTable(); DataSet1.CHILDRow row = dt.NewCHILDRow(); row.ID = 1; row.PARENTID = 1; row.NAME = "Child 1"; dt.AddCHILDRow(row); CHILDTableAdapter cta = new CHILDTableAdapter(); // cta.Transaction = transaction; Not allowed because you can't convert source type 'System.Data.Common.DbTransaction to target type 'System.Data.OracleClient.OracleTransaction. cta.Update(dt); // The thread will encounter a deadlock here, waiting for a write lock on the Child table. transaction.Commit(); } } Console.WriteLine("Successfully inserted parent and child rows."); Console.ReadLine(); } } } </code></pre> <hr> <ul> <li>As the comments above indicate, the thread will halt indefinitely on the child data adapter's update call because it will wait indefinitely for a write lock on the Child table. [Note the foreign key relationship: Parent.Id -> Child.ParentId (M:N)]</li> </ul> <p>Here's my question:</p> <ul> <li>I want to wrap the entire code block in a transaction.</li> <li>Can I do this? Considering that: <ul> <li>I want to commit an update on the Parent table with Linq-to-Sql's <strong>SubmitChanges method</strong>...</li> <li>And I want to commit an update on the Child table with an ADO.NET dataset <strong>table adapter</strong>.</li> </ul></li> </ul> <hr> <p>Here are two interesting footnotes:</p> <ol> <li>This whole things <em>works</em> in reverse. That is, if I wanted to submit changes to the parent table with a data adapter and changes to the child table with linq-to-sql... that <em>would work</em>.</li> <li><p>I tried to explicitly attach the transaction to the dataadapter, but the compiler won't allow it because it is a different type of transaction.</p> <pre><code> CHILDTableAdapter cta = new CHILDTableAdapter(); cta.Transaction = transaction; // Not allowed because you can't convert source type 'System.Data.Common.DbTransaction' to target type 'System.Data.OracleClient.OracleTransaction'. cta.Update(dt); transaction.Commit(); </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload