Display foreign key attribute error

0

I have my 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; }
}

The method of my repository:

 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;
    }

My 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);

Then my 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" })
}

Generate the following error in 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 provisioned   and can not be used for operations that require a connection.

    
asked by anonymous 11.09.2014 / 21:31

1 answer

1
Your View possibly does lazy loading of something on your object, so the requirement for the context to exist until the rendering of View .

Manual% wrap is not necessary, since it is done by itself at the end of dispose and Action execution.

    
12.09.2014 / 00:15