This question is locked. New answers and comments are not allowed.
Hello.
I have a method that return a complex type (from a stored procedure), that is working fine.
But now, I decided to change this method to return an IQueryable object, but, when I try to do this, I get the following error:
My code is:
My Parent REP class:
I'm using the UnitOfWork pattern:
My calling in aspx.cs page:
One import point is:
The problem occurs only if I try to set the grdComunicacao.DataSource, if I use a variable instead of grdComunicacao.DataSource I get no errors. Ex:
Var myvar = oUnitOfWork.CanalComunicacaoREP.BuscarPorEntidade(1);
I have debuged the code, and, my dbContext is not null, even my result is not null.
What I'm doing wrong?
Regards.
I have a method that return a complex type (from a stored procedure), that is working fine.
But now, I decided to change this method to return an IQueryable object, but, when I try to do this, I get the following error:
The ObjectScope has already been disposed and it's managed persistent objects can no longer be accessed. The ObjectScope should be disposed at the end of the life cycle of your business logic instance. This can also be done in the Dispose of your ASP page or MVC controller.
Nome do objeto: 'OpenAccessRuntime.EnlistableObjectScope'.
My code is:
public class CanalComunicacaoREP : Repositorio<CanalComunicacao>, IDisposable
{
private
EntitiesModel dbContext;
public
CanalComunicacaoREP(EntitiesModel context) :
base
(context) {
this
.dbContext = context; }
public
IQueryable BuscarPorEntidade(
int
pEntidadeID)
{
var result = from cc
in
dbContext.CanalComunicacaos
join cct
in
dbContext.CanalComunicacaoTipos on cc.CanalComunicacaoTipoID equals cct.CanalComunicacaoTipoID
where cc.EntidadeID == pEntidadeID
select
new
{
cc.CanalComunicacaoID,
cc.Descricao,
cc.IsPrincipal,
cct.CanalComunicacaoTipoID,
TipoDescricao = cct.Descricao
};
return
result.AsQueryable();
}
//Only for knowing, below is the method using the complex type, that works with no erros:
public List<result_BuscarCanaisPorEntidade> BuscarPorEntidade(int pEntidadeID)
{
return dbContext.Stp_BuscarCanaisPorEntidade(pEntidadeID).ToList();
}
}
My Parent REP class:
public
abstract
class
Repositorio<T> : IRepositorio<T>
{
private
EntitiesModel dbContext;
public
Repositorio(EntitiesModel context)
{
this
.dbContext = context;
}
//methods
}
I'm using the UnitOfWork pattern:
public class UnitOfWork : IDisposable
{
private readonly EntitiesModel _context;
private CanalComunicacaoREP _canalComunicacaoREP;
public UnitOfWork()
{
_context = new EntitiesModel();
}
public CanalComunicacaoREP CanalComunicacaoREP
{
get
{
if (this._telefoneREP == null)
this._canalComunicacaoREP = new CanalComunicacaoREP (_context);
return _canalComunicacaoREP;
}
}
public void Save()
{
_context.SaveChanges();
}
My calling in aspx.cs page:
protected void grdCanalComunicacao_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
if (Request["EntidadeID"] != null)
{
using (UnitOfWork oUnitOfWork = new UnitOfWork())
{
grdCanalComunicacao.DataSource = oUnitOfWork.CanalComunicacaoREP.BuscarPorEntidade(1);
}
}
}
One import point is:
The problem occurs only if I try to set the grdComunicacao.DataSource, if I use a variable instead of grdComunicacao.DataSource I get no errors. Ex:
Var myvar = oUnitOfWork.CanalComunicacaoREP.BuscarPorEntidade(1);
I have debuged the code, and, my dbContext is not null, even my result is not null.
What I'm doing wrong?
Regards.