Hello, How to do a simple sql query withing the dbContext. I mean everything we usually used do not work: dbContext.Database.SqlQuery is not available. Why is this not available? How do to it?
Regards
5 Answer(s)
-
0
@SquaresUnion,
Are you using EF 6.x or EF Core ? Can you share the query you have tried and the error message ?
-
0
Hello I am using EF Core. I need to inject the dbcontext in my service to perform custom sql queries. basically to query sequence. Do u know how to do it?
-
0
Whats the query you are trying to run?
-
0
I found it for others you need to inject
IDbContextProvider<AbpDbContext> _dbContext;
then u canhave the dbcontext doing
_dbContext.GetDbContext()
Yeeehh!!!
-
0
You can also create a custom repository for the entity. In custom repositories there's a shortcut method GetDbContext() to retrieve your DbContext.
public class SubscriptionPaymentRepository : AbpZeroTemplateRepositoryBase<SubscriptionPayment, long>, ISubscriptionPaymentRepository { public SubscriptionPaymentRepository(IDbContextProvider<AbpZeroTemplateDbContext> dbContextProvider) : base(dbContextProvider) { } public async Task UpdateMyDbWithSqlCommand() { await GetDbContext().Database.ExecuteSqlCommandAsync("Update myTable set MyField=1 where MyFilter=2"); } }