Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have used several context within one class. I have not used any global context. I need each of my context to stay open as I am doing multiple queries and saving more than once in the same method before disposing the context. Initially I declared as below

context = new Entities();

multiple queries..

save..

query...

context.Dispose();

The problem with this is, entity framework closes the connection after save() command. Then I decided to do following:

context = New Entities(); context.Database.Connection.Open()

multiple queries..

save..

query...

context.Database.Connection.Close() context.Dispose();

This works fine in my development machine. As soon as I try the same program in a different machine, it behaves very strange. Sometimes it takes 5-6 minutes to open and in sometimes 20-25 minutes. But once it starts everything else works fine.

As it is working fine in my development machine then I am missing something. I have been trying for last few days without any luck.

Any help will be much appreciated.

Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 24-Sep-13 20:48pm    
Well, it may depend on many factors. Do you have stable network connection (you can ping to see)? Is the database server busy with other clients? We really don't have enough information to help you more.
—SA
adat7378 24-Sep-13 22:20pm    
Ping is fine. Less than a millisecond. The strange thing is, the behaviour is same even if the database is on the same machine. I have done all sort of test on the network. I thought after I opened the database it is not getting closed somewhere. Sorry I forgot to mention that it works fine first time. I have closed the database and disposed the context wherever I used it. Is there any additional step needs to be taken to dispose that context when exiting the application?

1 solution

instead of using you context like this

context = new Entities();
multiple queries..
save..
query...
context.Dispose();


use like this

using ( var context = new Entites())
{ //starting here connection opens
multiple queries..
save..
query...
} // before this bracket the connection is closed

so you should be able to do any queries updates or whatever as long as you inside the above block.

also no you have no need to manually dispose of context thats now handled for you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900