locked
Is possible to use TransactionScope in Entity Framework using DataBase First Approach. RRS feed

  • Question

  • Hi,

    I am starting new project and decided to go Entity Framework 6.1 database approach. I am plan to use server side Transaction. So Please find the following my queries which i have to clear before starting the project.

    1. Is there any possible to use TransactionScope class in EF using DataBase First Approach. I can able to achieve Transaction class with commit and rollback method.

    2. What is the main advantages of TransactionScope instead of using Transaction.

    3. EF its supporting TransactionScope class? Thanks.

    Tuesday, April 8, 2014 5:26 AM

Answers

  • I have never used Transaction in EF only Transaction.Scope with a using statement. Using Transactions in a using statement scope in the cleaner solution.

    I used Transaction way back in my COM VB6 days.

    Don't use Transaction.Scope in logic that has a connection open within a open connection, because it is going to blow with exception stating that the machine the code is running on must have MSDTC enabled on the machine, which is not feasible in all situation. 

    Tuesday, April 8, 2014 7:16 AM

All replies

  • It doesn't matter if it's  the database, model or code first in EF the EF code for CRUD operations can be encompassed within a System.Transaction.Scope.

         private void AddAuthor(DTOAuthor author)
            {
                using (var conn = new EntityConnection(Constants.pcdb))
                using (var db = new PublishingCompanyEntities(conn))
                using (var transScope = new TransactionScope())
                {
                    try
                    {
                        var auth = new Author
                                       {
                                           FirstName = author.FirstName.Trim(),
                                           LastName = author.LastName.Trim(),
                                           Payroll = new EntityCollection<Payroll>(),
                                           Article = new EntityCollection<Article>()
                                        };

                        var payroll = new Payroll {Salary = 0};

                        auth.Payroll.Add(payroll);

                        var art = new Article {Title = "new"};

                        auth.Article.Add(art);

                        db.AddToAuthor(auth);
                        db.SaveChanges();

                        transScope.Complete();
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }

    Also take note on this if EF is being used a in an online application with multiple users to prevent deadlocks.

    http://blogs.msdn.com/b/diego/archive/2012/04/01/tips-to-avoid-deadlocks-in-entity-framework-applications.aspx

    You can post to the below forum if you have anymore questions about EF.

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

     
    Tuesday, April 8, 2014 6:08 AM
  • Yes it is possible, but it's not recommended at all. Read this article to understand why.



    Please mark the post as an answer that helps/solves your problem.

    Tuesday, April 8, 2014 6:17 AM
  • Thanks for your valuable information. Now i got the solution. But i have one more doubt like Is there any differences in TransactionScope and Transaction? while using in EF. Thanks.
    Tuesday, April 8, 2014 6:40 AM
  • I have never used Transaction in EF only Transaction.Scope with a using statement. Using Transactions in a using statement scope in the cleaner solution.

    I used Transaction way back in my COM VB6 days.

    Don't use Transaction.Scope in logic that has a connection open within a open connection, because it is going to blow with exception stating that the machine the code is running on must have MSDTC enabled on the machine, which is not feasible in all situation. 

    Tuesday, April 8, 2014 7:16 AM