C# 错误:ObjectContext 实例已被释放,不能再用于需要连接的操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17693145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:12:11  来源:igfitidea点击:

error:The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

c#wpfentity-framework

提问by rockenpeace

i have a listbox and when i select an item from this listbox called as ListofKBrands1, i take this error message:

我有一个列表框,当我从这个名为 ListofKBrands1 的列表框中选择一个项目时,我收到以下错误消息:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

ObjectContext 实例已被释放,不能再用于需要连接的操作。

In code-behind,place of this error:

在代码隐藏中,此错误的位置:

if (co.Company != null)

my code:

我的代码:

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;
        }
    }

before error:

错误前:

enter image description here

在此处输入图片说明

my KBrand.cs:

我的 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:

公司.cs:

public class Company
{
    [Key]
    public int Id { get; set; }
    public String Name { get; set; }

    public override string ToString() {
        return Name;
    }
}

if company of selected KBrand is null, this error does not appear. but if company of selected KBrand is not null, i take this error.how can i fix this error ? thanks in advance.

如果所选 KBrand 的公司为空,则不会出现此错误。但是如果选定的 KBrand 公司不为空,我会接受这个错误。我该如何解决这个错误?提前致谢。

采纳答案by Sergey Berezovskiy

Companyshould be lazy-loaded in your case. But your entity now in detached state (context which loaded KBrandentity now disposed. So, when you are trying to access Companyproperty, Entity Framework tries to use disposed context to make query to server. That gives you exception.

Company在你的情况下应该是延迟加载的。但是您的实体现在处于分离状态(加载KBrand实体的上下文现在已释放。因此,当您尝试访问Company属性时,实体框架会尝试使用已释放的上下文向服务器进行查询。这会给您带来异常。

You need to attach your KBrandentity to current context in order to enable lazy-loading

您需要将KBrand实体附加到当前上下文以启用延迟加载

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

OR you need to use eager loading, to have Companyalready loaded. Something like this when you getting items:

或者你需要使用预先加载Company已经加载。当你拿到物品的时候是这样的:

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

回答by sriharsha KB

To just add to Sergey's point,

补充一下谢尔盖的观点,

Instead of this,

取而代之的是,

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

This worked for me..

这对我有用..

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