Base solution for your next web application

Activities of "paladyr"

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.

Does anyone know of a sample that shows how to add a method to the repository that returns a list of objects using a SqlQuery? Thanks!

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!

So I already looked at the MultipleDbContextDemo solution and I don't get what I'm doing wrong. Here's what I've done:

I created a new AbpDbContext for the second connection. I created another RespositoryBase (not sure if that's necessary) for this new AbpDbContext The repository I want to use with the second dbcontext uses the new RepositoryBase and DbContext

When the repository that I want to use with the second dbcontext is called it's using the default (first) dbcontext. What am I doing wrong? Thanks!

I created two AbpDbContext classes but I don't know how to tell my repository to use the second DbContext. I looked at the MultipleDbContextDemoDataModule project but I still don't understand how it knows to use the MySecondDbContext for Courses. The repository I want to use the second DbContext is currently pulling in the first (default) one. Thanks!

Showing 1 to 9 of 9 entries