I answered my own question here for anyone curious:
<a class="postlink" href="http://stackoverflow.com/questions/41966952/objectcontext-instance-has-been-disposed-async-method-tolistasync-sqlquery">http://stackoverflow.com/questions/4196 ... c-sqlquery</a>
I just had a thought, is it disposed because it's an asynchronous method? If that's the case how can I make this method be synchronous?
Just saw this which makes me think what I'm doing in one line of code should work:
<a class="postlink" href="http://stackoverflow.com/questions/27214182/tolistasync-in-a-dbcontext-using-statement-the-objectcontext-disposed-how">http://stackoverflow.com/questions/2721 ... sposed-how</a>
Ok so caught the error and apparently the DbContext is destroyed by the time it tries to convert it to a list.. still working through it.
Error is this: "the objectcontext instance has been disposed."
I really miss just using datatables at this point what a pain. Still not sure how I should resolve this. I even tried making it all one line of code:
return await Context.Database.SqlQuery<MyObj>(sql).ToListAsync();
How can it be disposed before it finishes what it's supposed to do, it's one like of code!!! lol.
And now the line result.ToListAsync() is throwing an error again what the heck???? I didn't change any code?
Update: It seems to only work when debugging/stepping through code. What am I doing wrong?
Figures it would work a few minutes after I posted this lol. I do have questions though:
I'm doing this:
public async Task<List<MyObj>> GetObjects(){
string sql = "sql statement";
var result = Context.Database.SqlQuery<MyObj>(sql);
var listResult = await result.ToListAsync();
return listResult;
}
Do I have to do this as an async method? result only has ToListAsync() not .ToList() so that's why I made it async. I guess it probably should be but was curious if it had to be.
Sorry for the multiple posts, since I didn't see my first post show up I thought it didn't work but then discovered that my first post has to be approved before it shows up!
Anyway while I was waiting for my post to be approved I got it working and I'm not sure why it made it work. So previously I defined my AbpDbContext with these constructors:
public MyDbContext() : base("connString")
public MyDbContext(DbConnction existingConnection) : base(existingConnection, false)
public MyDbContext(DbConnection existingConnection, bool contextOwnsConnection) : base(existingConnection, contextOwnsConnection)
When it was setup like that, my repository was using the default connection string in the default DbContext instead of connString in MyDbContext. As soon as I removed the last two constructors, my repository started using the right connection string (connString) and not the default one. Does that make sense why that was happening? Maybe the 2nd and 3rd constructors force this DbContext to use an existing connection instead of the connection I'm telling it to use?
Thanks for the rseponse!