-1

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.

paladyr
  • 1
  • 1
  • 4
    How are you creating/disposing your context? Show that please. – DavidG Jan 31 '17 at 20:34
  • in the constructor I have: public MyRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) Then I have a AbpDbContext class: public class MyDbContext : AbpDbContext{ public MyDbContext() : base("Default"){} public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString){} } – paladyr Jan 31 '17 at 20:37
  • 1
    That latest edit does not help much and is not what DavidG asked for. You are showing your constructors but nothing about the life cycle for the context. – Igor Jan 31 '17 at 20:43
  • The issue arises because of delayed execution of the query - the query is not execyted until the .`To.ListAsync()` occurs by which time the SQLQuery method has executed and disposed it's connection. The "best" (and it's not very good IMO) solution I have seen for this is to NOT wrap your DB Context/connection in a using statement and just rely on the GC to clean up. – Theo Jan 31 '17 at 20:46
  • I would show more but I'm not sure there's anything to show? Have you worked with an asp.net boilerplate solution before? I think it handles all this stuff in the background, which is one thing I hate about it. I would rather write more lines of code and be able to see what's going on. I added a link to the place where I generated the template for the project. You can download it and see for yourself how it works. Thanks for the responses! – paladyr Feb 01 '17 at 14:42

1 Answers1

0

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();
    }
}
paladyr
  • 1
  • 1