Tenho as minhas classes:
public class Bandeira
{
public int BandeiraID { get; set; }
public string Nome { get; set; }
public virtual ICollection<Curso> Cursos { get; set; }
}
public class Curso
{
public int CursoID { get; set; }
public virtual int BandeiraID { get; set; }
public virtual Bandeira Bandeira { get; set; }
}
O método do meu repositório:
public IQueryable<TEntity> Get(Func<TEntity, bool> predicate, string[] includes)
{
var query = GetAll().Where(predicate).AsQueryable();
foreach (var include in includes)
{
query = query.Include(include);
}
return query;
}
Meu Controller:
var bdCurso = new CursoRepositorioEF(contexto);
var curso = bdCurso.Get(x => x.CursoID == id, new string[] { "Bandeira" }).FirstOrDefault();
bdCurso.Dispose();
if (curso == null)
return RedirectToAction("Index");
return View(curso);
Então minha View:
@model Aplicacao.Core.Dominio.Curso
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.CursoID)
@Html.Display("teste", Model.Bandeira.Nome, new { @class = "form-control", @disabled = "disabled" })
}
Gera o seguinte erro na View:
An exception of type 'System.ObjectDisposedException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.