This project is read-only.

Suppressing ambient transactions

Topics: Customizing Orchard
Sep 28, 2012 at 12:52 PM


I'm an Orchard newbie, and I have a newbie question.

I am converting an existing mvc application to Orchard, and in order to practice for the real thing, I am trying to convert the contoso university sample MVC app to Orchard to learn the basics.

I have been through creating a module for it, adding menu elements to access the app, and I have also been able to display the output from the Home\Index action, so that I know that the module is set up correctly.

Now I am trying to add the Home\About action, which will display a table that is produced from a database query.

This gives me the following error:

MSDTC on server 'DEV\SQLEXPRESS' is unavailable.


An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: MSDTC on server 'DEV\SQLEXPRESS' is unavailable.

Source Error:

Line 17:     </tr>
Line 18: 
Line 19: @foreach (var item in Model) {
Line 20:     <tr>
Line 21:         <td>

Source File: c:\Users\dev1\Documents\My Web Sites\Orchard CMS\Modules\ContosoUniversity\Views\Home\About.cshtml Line: 19


Now, after some googling, I understand that this has something to do with the need for suppressing ambient transactions. So I add the following code to my home controller:



public class HomeController : Controller
        private SchoolContext db = new SchoolContext();
        public ActionResult Index()
            ViewBag.Message = "Welcome to Contoso University!";
            return View();
        public ActionResult About()
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress))
                var query = "SELECT EnrollmentDate, COUNT(*) AS StudentCount "
                    + "FROM Person "
                    + "WHERE EnrollmentDate IS NOT NULL "
                    + "GROUP BY EnrollmentDate";
                var data = db.Database.SqlQuery(query);
                return View(data);

This does not help. I dont fully understand the process here, I guess the transaction is probably already listed, but I don't know why, or how to prevent it.

Please help!

Sep 30, 2012 at 9:38 PM

Note that what you're sending to the view is a query object, not the resulting dataset. That means that the querying actually happens in the view, not in you controller action, and so out of your using block. Just ToList it before you return.