8

Ok, I give up...

What I want is to share the EF4's DbContext instance per request. I configured StructureMap like this:

For<MyContext>().Use(new MyContext("LocalhostConnString"));

But when I refresh my site, or even open it in another browser, I get the same exact instance of MyContext. Why is this shared across requests?

Am I missing something?

1 Answer 1

14

Yes... about 4 characters. Try:

For<MyContext>().Use(() => new MyContext("LocalhostConnString"));

If you give StructureMap an object instance, it will treat that instance as a singleton and return the same one every time. If instead you give it a lambda that creates an instance, it will run that lambda each time the type is requested.

3
  • 1
    So, "each time the type is requested", but not once per request? Commented Aug 18, 2011 at 2:38
  • 1
    @Roman Royter Exactly my question. How should I do it per request?
    – forhas
    Commented Mar 19, 2013 at 13:37
  • the default lifecycle is Transient and that means a nested container for MVC / Web API which effectively means per request - apparently Commented Jan 9, 2018 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.