【发布时间】: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