Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"connection's current state is open" #3240

Closed
endink opened this issue Sep 26, 2015 · 2 comments
Closed

"connection's current state is open" #3240

endink opened this issue Sep 26, 2015 · 2 comments

Comments

@endink
Copy link

endink commented Sep 26, 2015

When i use inject a dbcontext in a controller, and execute multi threaded query, like this:

Task t1 = query 1(ToArrayAsync());
Task t2 = query 2;
Task t3 = query 3;

Task.WhenAll(t1, t2, t3);

Often an error occurred:
A database operation failed while processing the request.

"connection's current state is open"

There are pending model changes for ApplicationDbContext
Scaffold a new migration for these changes and apply them to the database from the command line:

dnx . ef migration add [migration name]
dnx . ef migration apply

@divega
Copy link
Contributor

divega commented Sep 26, 2015

@endink DbContext is not thread-safe and interleaving query execution like this on the same instance is not supported. The error condition you are running into ("connection's current state is open") is possibly the most innocuous problem you may have if you do this. Any state maintained by the DbContext can as well be corrupted.

DbContext has never been thread safe in any previous version of EF. In EF6 we added guards in the async APIs to throw a specific exception when they were used in this way. We should probably consider doing this again in EF7.

On the other hand, I am not sure how the part about "pending model changes" is related. Does that go away if you serialize your async queries (e.g. awaiting each individual call to ToArrayAsync())?

@endink
Copy link
Author

endink commented Sep 30, 2015

thanks, so i use "await" it works fine, "pending model changes" is a bug in my code, sorry . thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants