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 6361971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:50:48+00:00 2026-05-24T23:50:48+00:00

I use StructureMap, EF 4.1/POCO. Console app supposes to run 2 consequent operations upon

  • 0

I use StructureMap, EF 4.1/POCO.
Console app supposes to run 2 consequent operations upon some set of data, lets say operation1 and operation2. I set DbContext up as an singleton. This causes problem in operation2 as operation1 left some trash in its DbContext that prevent operation2 works well. In the same time I can not set up DbContext as ‘per call’ coz operation1 uses 2 repositories sharing the same DbContext passing through their constructor. So ideally I need reinitialize/reset/cleanup DbContext before operation2. Any ideas?

Thanks

  • 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-05-24T23:50:49+00:00Added an answer on May 24, 2026 at 11:50 pm

    Simply use two different contexts. There is no better solution to reset context then creating a new one. If you are fighting with your current architecture simply improve it to support new scenario. Instead of passing context instance pass a context factory which will be able to create you as many context instances as you need. Same with repositories – you can have factory to create a new repository instances on demand.

    Edit with example:

    Let’s suppose that you are using EFv4.1 Update 1. It offers new interface IDbContexFactory<TContext>. You can define your class this way:

    public class YourClass
    {
        private readonly IDbContextFactory<IYourContext> _factory;
    
        public YourClass(IDbContextFactory<IYourContext> factory) 
        {
            _factory = factory;
        }
    
        public void Operation1() 
        {
            using (IYourContext context = _factory.Create()) 
            {
                RepositoryA repository = new RepositoryA(context);
                RepositoryB repository = new RepositoryB(context);
                ...
            }
        }
    
        public void Operation2()
        {
            using (IYourContext context = _factory.Create()) 
            {
                RepositoryA repository = new RepositoryA(context);
                RepositoryB repository = new RepositoryB(context);
                ...
            }
        }
    }
    

    This was example where you pass factory for context but you can do the same for repository if you want to.

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

Sidebar

Related Questions

I've implemented some components to use WCF with both an IoC Container (StructureMap) and
I am trying to use StructureMap to register some types that implement a generic
I'm adapting some code originally written for Windsor to use StructureMap. In the Windsor
I see a lot of examples of how to use StructureMap in a asp.net
Use case: I've just entered insert mode, and typed some text. Now I want
use Rack::Static, :urls => ['/stylesheets', '/images'], :root => 'public' run proc { |env| [200,
I am not sure how to use StructureMap to scan for all repositories in
How can I figure StructureMap to use more than one concrete Class per Interface.
i've been using structuremap since a couple of months. I always use ObjectFactory.GetInstance to
I'm using StructureMap 2.6.1 This is the code from Bootstrapper.cs: ObjectFactory.Initialize(x => x.For<IFoo>().Use<Foo>()); When

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.