实体框架错误 - 同时读取数据库 [英] entity framework error - simultaneous db reads

查看:92
本文介绍了实体框架错误 - 同时读取数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC页面,从数据库中读取数据。另外在页面上,我有一些图像链接到/ myController的/照片这也使得在一些数据库读取。现在照片被浏览器获取的同步,所以我发现一些照片没有显示出来,我也记录一些错误:

I have a MVC page that reads data from db. Also on the page I have some image links to /MyController/Photo which also makes some reads on the db. Now the photos are being fetched by the browser "simultaneously" so I notice that some photos don't show up and I also logged 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()
......

下面是我的code:
在视图:

Here is my code: In the view:

<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)
        {
        ...
        }
   }

我的问题我怎么能同步到上下文实体框架的访问,以确保我没有得到这些错误?

My question how can I synchronize the entity framework access to the context to make sure I don't get these errors?

推荐答案

我这样做与IOC /依赖关系注入以及传递到每个库一个单DatabaseFactory。该DatabaseFactory类持有该网站的唯一的DbContext,这样一来,只有一个,不进入任何奇怪的问题。而且我也注入我的仓库到我的控制器..我只是懒惰这样。

I do this with IoC/Dependancy Injection and a singleton DatabaseFactory that gets passed into each repository. The DatabaseFactory class holds the only DbContext for the site, this way there is only one and don't get into any weird issues. And I also inject my repositories into my controllers.. I'm just lazy like that.

DatabaseFactory

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(我所有的respositories继承)

BaseRepository (that all my respositories inherit from)

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类,但你可以不用的。希望这有助于。

I actually have a UnitOfWork class in the middle, but you can do it without that. Hope this helps.

这篇关于实体框架错误 - 同时读取数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆