我有一个MVC页面,从数据库中读取数据。此外,在页面上我有一些图片链接指向/MyController/Photo,这也会在数据库上进行一些读取操作。现在,浏览器"同时"获取照片,所以我注意到有些照片没有显示出来,我还记录了一些错误:
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()
......
这是我的代码: 在视图中:
<img src="@Url.Action("Photo", "Profile", new { type = "listing", uID = f.uID })" />
在控制器操作中:
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)
{
...
}
}
我的问题是如何同步实体框架访问上下文,以确保我不会遇到这些错误?