我有很多的班级,这样的结构可以看到:EF返回ExecuteReader需要打开并且可用的Connection。连接的当前状态已打开。错误
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.
我想知道我应该使用这个,如果是的我怎么能改变我的类结构使用在我的代码?
我想这在oop中是不正确的。你可以分享你的依赖关系映射器吗? –
但它解决了我的问题 –
哪部分不正确? –