ho un listbox e quando i selezionare un elemento in questo listbox chiamato come ListofKBrands1, prendo questo messaggio di errore:errore: L'istanza ObjectContext è stato eliminato e non può più essere utilizzato per operazioni che richiedono una connessione
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Nel code-behind, luogo di questo errore:
if (co.Company != null)
il mio codice:
private void ListofKBrands1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
RSPDbContext c = new RSPDbContext();
if (ListofKBrands1.SelectedItem != null)
{
ListBoxItem item = ListofKBrands1.SelectedItem as ListBoxItem;
KBrand co = item.Tag as KBrand;
if (ListofKBrands1.SelectedItem != null)
txtNewKBrand.Text = co.Name;
else
txtNewKBrand.Text = "";
int count = 0;
if (co.Company != null)
{
foreach (string a in cbCompany.Items)
{
if (a == co.Company.Name)
cbCompany.SelectedIndex = count;
count++;
}
}
else
cbCompany.SelectedIndex = 0;
}
}
prima di errore:
miei KBrand.cs:
public class KBrand {
[Key]
public int Id { get; set; }
public String Name { get; set; }
public virtual Company Company { get; set; }
public override string ToString() {
return Name;
}
}
company.cs:
public class Company
{
[Key]
public int Id { get; set; }
public String Name { get; set; }
public override string ToString() {
return Name;
}
}
se la società di selezionati KBrand è nullo, non appare questo errore. ma se la compagnia di KBrand selezionata non è nullo, prendo questo errore. Come posso risolvere questo errore? Grazie in anticipo.
@rockenpeace codice aggiunto per entrambe le opzioni –
vi ringrazio molto .. ho provato il primo suggerimento e funziona. Ho provato il secondo su caricamento avido ma non riconosciuto b. – rockenpeace
@rockenpeace metodo generico definito nello spazio dei nomi 'System.Data.Entity.DbExtensions'. Inoltre puoi usare 'Include (" Azienda ")' invece –