You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 10, 2024. It is now read-only.
Multiple DbContexts select query can't work concurrently. Please see the code below.
BTW, if I use async query, "System.ObjectDisposedException : Cannot access a disposed object." will occur.
`
public async Task ConcurrentTest()
{
var tasks = new List<Task>();
for (int i = 0; i < 20; i++)
{
var j = i;
tasks.Add(Task.Run(() => InternalGetUsersTest(j)));
}
await Task.WhenAll(tasks);
}
private void InternalGetUsersTest(int i)
{
var builder = new DbContextOptionsBuilder().UseMongoDb("mongodb://10.100.7.46:9007/DemoDb");
using (var dbContext = new DemoDbContext(builder.Options))
{
var database = dbContext.GetMongoDbDatabase();
// !!! 20 concurrent tasks cost 3 seconds.
var user1 = database.GetCollection<User>("users")
.Find(new ExpressionFilterDefinition<User>(u => u.Id == "xxx"));
// !!! 20 concurrent tasks cost almost 20 seconds. It seems each task run one by one.
var user2 = dbContext.Users
.FirstOrDefault(u => u.Id == "xxx");
}
}`