Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason not to use nested using EF entities contexts?

using (var context = new FirstEntities())
{
   using (var context1 = new SecondEntities())
   {
   }
}

This works but for some reason doesn't "feel right"... Does anyone know any valid reason not to use nested using statements with entity framework?

Edit: My question is more along the lines if there is a scenario where this type of nesting could cause an exception or a database error rather than if it is advisable from architectural stand point...

like image 776
Dean Kuga Avatar asked Mar 21 '11 17:03

Dean Kuga


1 Answers

By nesting data contexts you will use two database connections at once. It's preferrable to get the data that you need from one context, close that and open the next, and get the data that you need from that context.

This might mean a little more work as you have to plan your code better, but it also means that the application scales better.

like image 181
Guffa Avatar answered Nov 01 '22 17:11

Guffa