以同样的DbContext第二个电话会导致错误“的DbContext已被释放” [英] Second call to same DbContext causes error 'DbContext has been disposed'

查看:2058
本文介绍了以同样的DbContext第二个电话会导致错误“的DbContext已被释放”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做

public void SomeTestMethod(){
    var person = this.PersonManager.Get(new Guid("someguid")); 

    person.Lastname = "Jameson";

    this.PersonManager.Save(person); // This goes wrong
}



如果它出错

以上保存方法调用此代码:

The above save method calls this code:

protected void Add<T>(T source, MyEntities context, bool isNew) where T : class
{
    if (isNew)
    {
        context.Set<T>().Add(source);
    }
    else
    {
        var entry = context.Entry(source);
        if (entry.State == EntityState.Detached)
        {
            context.Set<T>().Attach(source);

            entry.State = EntityState.Modified;
        }
    }
}



VAR进入= context.Entry(源); 行是在导致这个错误:

The var entry = context.Entry(source); line is the one causing this error:

操作因为的DbContext已被处置无法完成。

The operation cannot be completed because the DbContext has been disposed.

我见过的回答类似的问题的建议使用.ToList()(还是其他什么东西来执行的链接),但是这已经发生了,因为获取返回一个DTO对象。

I've seen answers to similar questions that suggested to use .ToList() (or something else to execute the link), but that has happened, because the Get returns a DTO object.

一些背景

Some background

的PersonManager 用于保存,使用此设置的DbContext:

The PersonManager used in save, uses this to set the DbContext:

var context = new MyEntities();
this.PersonRepository = repositoryProvider.GetRepositoryByType<PersonRepository>(context);

VAR背景=新MyEntities(); 只是为了得到它现在的工作,这将是DI注入

The var context = new MyEntities(); is just to get it working now, this will be DI injected.

而这又取决于这一点:

public T GetRepositoryByType<T>(MyEntities context) where T : IContextDependent
{
    var instance = this.Repositories.SingleOrDefault(x => x is T);

    instance.SetContext(context);

    return (T)instance;
}



由于相同的的PersonManager 时,实际上相同 PersonRepository 使用的(并因此同一个上下文中)的,所以我不明白为什么它会设置在第二个电话。

As the same PersonManager is used, de facto the same PersonRepository is used (and as a result the same context), so I don't see why it would be disposed on the second call.

推荐答案

您没有给你创建你的背景,其中的背景下,但我'M假设它是一个方法,也许是构造函数。上下文的作用域的方法,因此,GC是免费的,当方法调用结束处置它。我认为在所有工作,哪怕只有一次,但事实是,你设法打它它的垃圾回收之前。

You haven't given the context of where you're creating your context, but I'm assuming it's in a method, perhaps the constructor. Your context is scoped to that method, so the GC is free to dispose of it when the method call ends. I think the fact that it works at all, even just once, is that you're managing to hit it before it's garbage collected.

讽刺的是,你的问题发生,因为你不使用DI呢。简单地注入就足够有可能解决问题。最起码,你应该语境在同一级别为的PersonManager 作用域。

Ironically, your issue is occurring because you're not using DI yet. Simply injecting it would be enough likely to solve the issue. At the very least, your context should be scoped at the same level as PersonManager.

这篇关于以同样的DbContext第二个电话会导致错误“的DbContext已被释放”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆