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!
6 Answer(s)
-
0
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.
-
0
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?
-
0
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.
-
0
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>
-
0
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>
-
0
Hi @paladyr,
Sorry for my late response. Your first usage also seems correct it should work as well.
I have created a custom repository interface like this:
public interface IUserRepository : IRepository<User, long> { Task<List<User>> GetAllUsers(); }
and implemented it like this:
public class UserRepository : YourRepositoryBase<User, long>, IUserRepository { public UserRepository(IDbContextProvider<FlightPlannerDbContext> dbContextProvider) : base(dbContextProvider) { } public async Task<List<User>> GetAllUsers() { return await Context.Database.SqlQuery<User>("SELECT * FROM AbpUsers").ToListAsync(); } }
After that, I used this custom repository in my app service
public async Task<PagedResultDto<UserListDto>> GetUsers(GetUsersInput input) { var users = await _userRepository.GetAllUsers(); var userListDtos = users.MapTo<List<UserListDto>>(); return new PagedResultDto<UserListDto>( 10, userListDtos ); }
It worked as expected.
Can you explain your scenario in a bit more details ? Can you also share some more code ? We can test and fix if there is a problem. By the way, I have tried with ABP 1.2.2.