2013-07-17 10 views
5

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:

enter image description here

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.

risposta

19

Company dovrebbe essere pigro nel tuo caso. Ma la tua entità ora in stato distaccato (contesto che ha caricato l'entità Company, Entity Framework tenta di utilizzare il contesto disponibile per effettuare una query sul server .Questa è l'eccezione.

Tu necessità di collegare il soggetto KBrand al contesto attuale, al fine di consentire lazy-loading

RSPDbContext c = new RSPDbContext(); 
KBrand co = item.Tag as KBrand; 
c.KBrands.Attach(co); 
// use co.Company 

O è necessario utilizzare eager loading, di avere già caricato Company Qualcosa di simile a questo quando ottenere oggetti:.

RSPDbContext db = new RSPDbContext(); 
var brands = db.KBrands.Include(b => b.Company).ToList(); 
// assign brands to listbox 
+0

@rockenpeace codice aggiunto per entrambe le opzioni –

+0

vi ringrazio molto .. ho provato il primo suggerimento e funziona. Ho provato il secondo su caricamento avido ma non riconosciuto b. – rockenpeace

+0

@rockenpeace metodo generico definito nello spazio dei nomi 'System.Data.Entity.DbExtensions'. Inoltre puoi usare 'Include (" Azienda ")' invece –

1

Per aggiungere solo per il punto di Sergey,

Invece di questo,

RSPDbContext db = new RSPDbContext(); 
var brands = db.KBrands.Include(b => b.Company).ToList(); 
// assign brands to listbox 

Questo ha funzionato per me ..

RSPDbContext db = new RSPDbContext(); 
var brands = db.KBrands.Include("Company").ToList(); 
// assign brands to listbox 
+7

lambda tipizzato molto più meglio della costante con hardcoded – devi

+3

aggiungere using System.Data.Entity; perché l'espressione lambda funzioni –

+0

Cosa devo fare se 'Include' non aiuta affatto? Sto lottando con questo problema senza successo ... – Azimuth

Problemi correlati