bir bağlantısı gerektiren işlemler için kullanılabilir:hatası: ObjectContext örneği tanzim edilmiş ve artık ben bir liste vardır ve ben ListofKBrands1 olarak adlandırılan bu liste bir öğeyi seçtiğinizde, ben bu hata mesajını almaya
arka plan kod olarakThe ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
, bu hatanın yeri:
if (co.Company != null)
kodum:
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;
}
}
hata öncesi:
benim 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çilen KBrand şirket boşsa, bu hata görünmüyor. ancak seçilen KBrand'ın şirketi boş değilse, bu hatayı alıyorum. Bu hatayı düzeltebilir miyim? şimdiden teşekkürler. Durumunuzda tembel yüklü olması gerekir.
@rockenpeace çok teşekkür ederim –
her iki seçenek için kod eklendi .. ilk öneriyi denedim ve işe yarıyordu. İstekli yükleme konusunda ikincisini denedim ama b tanınmadı. – rockenpeace
@rockenpeace 'System.Data.Entity.DbExtensions' ad alanında tanımlanan genel yöntem. Ayrıca, Include ("Company") 'i kullanabilirsiniz –