Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF DbContext and StructureMap scoping

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?

like image 512
Darmak Avatar asked Sep 10 '10 22:09

Darmak


1 Answers

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.

like image 154
Joshua Flanagan Avatar answered Nov 20 '22 08:11

Joshua Flanagan