I get the following error when I want to iterate over a List
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I open the Form with the following code
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int id = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
Aufenthalt a;
using (var db = new GastContext())
{
a = db.Aufenthalte.First(x => x.AufenthaltID == id);
}
Aufenthaltsform frm = new Aufenthaltsform(currentGast, a);
frm.ShowDialog();
}
And this is the constructor of my form and here my Application throws the above error
public Aufenthaltsform(Gast g, Aufenthalt a)
{
InitializeComponent();
MessageBox.Show(a.Mitreisende.Count.ToString());
}
That's the Aufenthalt-Object
public class Aufenthalt
{
public int AufenthaltID { get; set; }
public DateTime Anreisedatum { get; set; }
public DateTime Abreisedatum { get; set; }
public virtual List<Mitreisender> Mitreisende { get; set; }
public virtual Gast Gast { get; set; }
public Aufenthalt()
{
Mitreisende = new List<Mitreisender>();
}
}