Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:58:01+00:00 2026-06-12T23:58:01+00:00

I am a EntityFramework newbie. I am facing a very weird issue. I am

  • 0

I am a EntityFramework newbie. I am facing a very weird issue. I am not being able to use transaction using a linq to entity query. I have used for DbTransaction as well as TransactionScope. The error occurs as soon as the query is hit. I am using EF 4.3. The context derives from DbContext as usual. I am using the Code First approach.

        using (TransactionScope s = new TransactionScope())
        {
         using(var context = new XYZContext())
         {   
               context.Database.Connection.Open();
               Int32 count = (from xyz in context.XYZs
                           where xyz.Name == "SameName"
                           select xyz.Name).Count();
               Assert.AreEqual(count, 1);
          }
        }

I have some doubts. I do not want to use the TrasactionScope because I have heard it has performance issues. So how can I use the DbTransaction instead(also throwing error for me)?
Why do I need to open the connection explicitly? When I am ‘newing’ up a context then should it not open the connection automatically for me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:58:02+00:00Added an answer on June 12, 2026 at 11:58 pm

    Try flipping your using clauses, and opening your connection before you start the transaction, and use the context to open the transaction. You also need to commit the transaction if your code is making updates.

    using(var context = new XYZContext())
      {   
          context.Database.Connection.Open();
          using (TransactionScope s = context.Connection.BeginTransaction())
          {
    
              Int32 count = (from xyz in context.XYZs
                               where xyz.Name == "SameName"
                               select xyz.Name).Count();
              Assert.AreEqual(count, 1);
              s.Commit()
            }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

newbie writing, I'm new to MVC 2 C# and using EntityFramework with LINQ. In
How to write an update query in EntityFramework using LINQ to Entities? EDIT: And
I am a newbie about entity framework and linq. My query is like that
I use EntityFramework and need assistance with LINQ query. Im building an application that
I am an Entity Framework newbie and am wondering if I am able to
Using: .Net 4.0 EntityFramework 4.3 IF I query a DBSet in this manner: public
I'm newbie with .NET Entity Framework 4.0 I'm using VS2010 and I created the
I'm a total newbie at Entity Framework and ASP.Net MVC, having learned mostly from
I am using EntityFramework 4.1. I have the following model: public class User {
I'm using EntityFramework code first with migrations. From the package manager console, I'm running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.