【问题标题】:EF returns ExecuteReader requires an open and available Connection. The connection's current state is open. errorEF 返回 ExecuteReader 需要一个打开且可用的连接。连接的当前状态是打开的。错误
【发布时间】:2017-09-15 14:41:42
【问题描述】:

如您所见,我有很多具有这种结构的类:

 public class OrganizationUserRepository : IOrganizationUserRepository
    {
        private  DataContext _ctx;

        public OrganizationUserRepository(DataContext ctx)
        {
            _ctx = ctx;
        }
        public bool Add(OrganizationUser entity)
        {
            try
            {
                _ctx.OrganizationUsers.Add(entity);
                _ctx.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                // TODO log this error
                return false;
            }
        }

        public bool Edit(OrganizationUser entity)
        {
            try
            {
                OrganizationUser Edited = _ctx.OrganizationUsers.Where(i => i.Id == entity.Id).First();
                _ctx.Entry(Edited).CurrentValues.SetValues(entity);
                _ctx.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                // TODO log this error
                return false;
            }
        }



        public bool Remove(string id)
        {
            try
            {
                Int64 Id = Int64.Parse(id);
                OrganizationUser obj = _ctx.OrganizationUsers.Where(i => i.Id == Id).First();
                _ctx.OrganizationUsers.Remove(obj);
                _ctx.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                // TODO log this error
                return false;
            }
        }


    }

构造函数中的 db 上下文是由 ninject 注入的。你可以看到它只是我的一个类。我在另一个使用单个 DB 的服务中有多个这样的类。(WCF 服务)。但我得到了这个错误在我的 wcf 跟踪日志中:

ExecuteReader requires an open and available Connection. The connection's current state is open.

我先使用 EF 代码。

我找到了这个Wrap DbContext db = new DbContext() inusing statement. 我想知道我应该使用这个,如果是的话,我该如何更改我的类结构以在我的代码中使用?

【问题讨论】:

标签: c# entity-framework wcf dbcontext using


【解决方案1】:

我使用了public Readonly DbContext。我只是删除了只读,一切正常。

【讨论】:

  • 我认为在 oop 中是不正确的。你能分享你的依赖映射器吗?
  • 我认为你的 Ninject 模块有问题。
  • 绑定().ToSelf().InScope(f => OperationContext.Current);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多