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

  • SEARCH
  • Home
  • 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 7813323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:48:58+00:00 2026-06-02T04:48:58+00:00

I refer specifically to the accepted answer for Impersonating user with Entity Framework ,

  • 0

I refer specifically to the accepted answer for Impersonating user with Entity Framework, which sports the following code:

using (((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate())
using (var dbContext = new MyEntityFrameworkContainer())
{
    ...
}

I would rather instantiate dbContext at only one place in my repositories, which implement IDisposable, and then dispose of the context when the entity is disposed of. I’m not sure how the two using scopes above affect each other though, so how can I achieve what this code does in terms of impersonation while avoiding the using blocks?

ADDED:
As answers below have suggested, I can simply use local variables and ‘manually’ ensure the resources are disposed, but my concern here is whether the instantiation in the inner using is in any way affect by the outer using. If this is only a matter of lifetime, and the outer using doesn’t establish any context or anything that affects the inner, then the below answers have answered my question.

  • 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-02T04:49:01+00:00Added an answer on June 2, 2026 at 4:49 am

    You can declare them as 2 private fields and instantiate them in the constructor.

    Then implement Dispose() and Dispose them in reverse order.

    And then of course the calling code (Business layer) should apply the using(){} pattern to Repository instances.

    Additional:

    The nested usings should not be important. Impersonate() is a state-change that affects the current thread. The using-implied Dispose() would call Undo().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with mouse events, specifically OnMouseWheel . Many code samples refer to distance
Please refer my previous question for code sample Sockets: sometimes (rarely) packets are lost
Specifically I refer to this example: <h:panelgroup> <a4j:support event=onlick action=do1/> <h:commandbutton id=1.../> <h:commandbutton id=2.../>
How can you refer to the combined entity created from an outer join within
How do I document a macro function in C++ using Doxygen, and refer to
I am trying to compile a code in C++ using code from : https://stackoverflow.com/questions/5953979/sending-and-receiving-array-using-mpi-part-2
I have an element $myElement that I want to refer to using this inside
Refer original post here for a reference to the original issue. What plugin should
To refer to the root of a website in asp I use an ASP
Please refer to this post. I have become able to configure my web.config file

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.