I'm working on an application which uses Breeze and Entity Framework 6, with the Unit of Work and Repository patterns. Currently, we're exploring the possibility of using a static UnitOfWork instance within the application. I've set this up in our controller by doing:
public class BreezeController : ApiController
{
private static readonly UnitOfWork _unit = new UnitOfWork();
...
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _unit.SaveChanges(saveBundle);
}
}
Here, the UnitOfWork class contains the EFContextProvider:
public class UnitOfWork
{
private readonly EFContextProvider<MyContext> _contextProvider = new EFContextProvider<MyContext>();
...
public SaveResult SaveChanges(JObject saveBundle)
{
return _contextProvider.SaveChanges(saveBundle);
}
}
This seems to work fine when querying the database and when saving new entities into the database. However, after the first call to saveChanges() in the front-end, any subsequent query to the database fails with the error: "Value cannot be null. Parameter name: connection". I've done a little digging and it seems that the connection to the database is reset once SaveChanges has completed (for example, EntityConnection.ConnectionString in the ContextProvider gets set to an empty string). I presume that this is what causes the error (and, of course, this isn't a problem when not using a static UnitOfWork since a new instance of the EFContextProvider will be created for the next query in that case).
Assuming this is the problem, is there a way to prevent the EFContextProvider from resetting the database connection when calling SaveChanges? Or is there a way that I can force it to re-establish the connection in time for the next query? On the other hand, is there some reason why I shouldn't be trying to use a static instance of the EFContextProvider?