tôi có một ListBox và khi tôi chọn một mục từ hộp danh sách này gọi là ListofKBrands1, tôi mất thông báo lỗi này:lỗi: Ví dụ ObjectContext đã được xử lý và có thể không còn được sử dụng cho các hoạt động yêu cầu kết nối
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
trong code-behind, nơi lỗi này:
if (co.Company != null)
mã của tôi:
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;
}
}
trước khi lỗi:
KBrand.cs tôi:
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;
}
}
nếu công ty của chọn KBrand là null, lỗi này không xuất hiện. nhưng nếu công ty của KBrand được chọn không phải là null, tôi lấy lỗi này. Làm thế nào tôi có thể sửa lỗi này? cảm ơn trước.
@rockenpeace thêm mã cho cả hai lựa chọn –
thank you very much .. Tôi đã thử đề nghị đầu tiên và nó Works.i đã cố gắng thứ hai về tải háo hức nhưng nó đã không được công nhận b. – rockenpeace
@rockenpeace generic method được định nghĩa trong không gian tên 'System.Data.Entity.DbExtensions'. Ngoài ra, bạn có thể sử dụng 'Bao gồm (" Công ty ")' thay vì –