public class TestBL
{
public static void AddFolder(string folderName)
{
using (var ts = new TransactionScope())
{
using (var dc = new TestDataContext())
{
var folder = new Folder { FolderName = folderName };
dc.Folders.InsertOnSubmit(folder);
dc.SubmitChanges();
AddFile("test1.xyz", folder.Id);
AddFile("test2.xyz", folder.Id);
AddFile("test3.xyz", folder.Id);
dc.SubmitChanges();
}
ts.Complete();
}
}
public static void AddFile(string filename, int folderId)
{
using (var dc = new TestDataContext())
{
dc.Files.InsertOnSubmit(
new File { Filename = filename, FolderId = folderId });
dc.SubmitChanges();
}
}
}
This is an example of nested DataContext (untested). The problem starts when a TransactionScope is added to our little experiment (as shown above). The first AddFile at the AddFolder function will escalate the transaction to DTC (which is bad by all means), because AddFile initializes new DataContext, thus opening a second connection to the DB.
- How can I use nested DataContext that will not occur a DTC usage?
- Is this all just plain wrong? Should I use the DataContext differently?