简体   繁体   中英

ObjectContext instance has been disposed, async method, ToListAsync(), SqlQuery

I've seen other questions similar to this but none close enough to help me figure this out. I'm getting the error here in my repository:

return await Context.Database.SqlQuery<MemberField>(sqlstring).ToListAsync();

How do I fix this? It has worked before without any changes to code so I know all I have to do is keep the context alive. I tried doing this:

Context.Configuration.LazyLoadingEnabled = false;

but it didn't help. This is inside an ASP.NET boilerplate project.

Thanks!

in the constructor I have:

public MyRepository(IDbContextProvider<MyDbContext>     dbContextProvider) : base(dbContextProvider)

Then I have a AbpDbContext class:

public class MyDbContext : AbpDbContext{
public MyDbContext() : base("Default"){}

public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString){}

}

You can grab a template solution just like the project I'm trying to work with from here: http://www.aspnetboilerplate.com/

Update: ABP handles creating and disposing database connections so I don't have any code to show that does that.

I was able to get this working but handling the dbcontext in a using statement, inside the repository. This prevents the Abp frameworking from handling the dbcontext and disposing it too early. So it currently looks like this:

public List<MyObjs> GetObjs(){
    using(var db = new MyDbContext()){
        var query = from x in db.mytable
                    select x;
        return query.ToList();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM