Entity structure error - reading db at the same time

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?

+4
source share
1 answer

IoC/Dependancy Injection singleton DatabaseFactory, . DatabaseFactory DbContext , , . . .

DatabaseFactory

public class DatabaseFactory : Disposable, IDatabaseFactory
{
    private CragDbContext _dataContext;
    private string _connectionString;

    public string ConnectionString
    {
        get { return _connectionString; }
        set { _connectionString = value; }
    }

    public DatabaseFactory(string connectionString)
    {
        if (connectionString == null) 
            throw new ArgumentNullException("connectionString");

        _connectionString = connectionString;
    }

    public CragDbContext Get()
    {
        if (string.IsNullOrEmpty(_connectionString))
            return _dataContext ?? (_dataContext = new CragDbContext());


        return _dataContext ?? (_dataContext = new CragDbContext(_connectionString));
    }

    protected override void DisposeCore()
    {
        if (_dataContext != null)
            _dataContext.Dispose();
    }
}

BaseRepository ( )

public class BaseRepository<T> : IRepository<T> where T : BaseEntity
{
    protected CragDbContext DbContext;
    protected readonly IDbSet<T> DbSet;

    protected IDatabaseFactory DatabaseFactory { get; private set; }

    protected CragDbContext Context
    {
        get { return DbContext ?? (DbContext = DatabaseFactory.Get()); }
    }

    #region Ctor

    public BaseRepository(IDatabaseFactory databaseFactory)
    {
        if (databaseFactory == null) 
            throw new ArgumentNullException("databaseFactory");

        DatabaseFactory = databaseFactory;
        DbSet = Context.Set<T>();
    }

    #endregion

    public virtual void Add(T entity)
    {
        DbSet.Add(entity);
    }

    .... etc
}

UnitOfWork , . , .

0

All Articles