Error display foreign key attribute

Asked

Viewed 112 times

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

So 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" })
}

Generates the following error in the 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.

1 answer

1


Your View possibly makes lazy load of something on its object, so the requirement for the context to exist until the complete rendering of the View.

The dispose manual is not necessary, since it is done alone at the end of the execution of the Action in the Controller.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.