I have an MVC page that reads data from db. Also on the page I have links to images in / MyController / Photo, which also do some reading on db. Now the photos are selected by the browser “simultaneously”, so I noticed that some photos are not displayed, and I also registered some errors:
System.NullReferenceException Object reference not set to an instance of an object.
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
at System.Data.EntityClient.EntityConnection.Open()
......
Here is my code: In view:
<img src="@Url.Action("Photo", "Profile", new { type = "listing", uID = f.uID })" />
In controller action:
public ActionResult Photo(string type, int uID)
{
User u = null;
if (uID == 0)
u = Repository.repository.GetUserByName(User.Identity.Name);
else
u = Repository.repository.GetUserByID(uID);
if (u != null)
{
...
}
}
My question is: how can I synchronize entity access to the context to make sure that I am not getting these errors?
source
share