开发者

Entity Framework and Load Testing

I am having a tough time to understand why this code is failing

I have a test method

IUnitOfWork unitofwork = EFUnitOfWork.CreateInstance();
IRepository<InformationRequest> informationRequestRepository = unitofwork.CreateRepository<InformationRequest>();
IEnumerable<InformationRequest> requests = informationRequestRepository.ToList();
unitofwork.Dispose();

EFUnityOfWork.CreateInstance calls the EFUnitOfwork Constructor
public EFUnitOfWork()
  {

     _currentContext = new MyDataContext();
  }

Here is the code for CreateRepository

public IRepository<T> CreateRepository<T>()
 {
     return new Repository<T>(_currentContext);
 }

The test above doesnt work on a load test. When i try to run i开发者_如何学JAVAt it says System.Data.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: The connection was not closed. The connection's current state is connecting.

I am disposing the context and creating a new one everytime. I dont understand where i am going wrong


Your code EFUnitOfWork.CreateInstance() is a static method.

When 2 threads call this at the same time they could get back the same context. Then you could get the error that you see.

You could fix it by locking such that it is only called by one thread at a time. But time would introduce a performance bottleneck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜