How do you pass variables to DbContext options if using it as a [service]
?
For example if you wanted to create a global filter for UserId you would make DbContext require UserId variable like so...
FooDbContext.cs
public FooDbContext : DbContext {
private static int _userId;
public FooDbContext(Id userId, DbContextOptions options)
: base(options)
{
_userId = userId;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>()
.HasQueryFilter(filter => filter.UserId == _userId)
}
}
Easy enough to use it like this...
[HttpGet]
public Task<List<Bar>> GetBarAsync(Id userId) {
FooDbContext context = new FooDbContext(userId)
return await context.Bar.ToListAsync();
}
But what about this?
[HttpGet]
public Task<List<Bar>> GetBarAsync(Id userId,
[Service] FooDbContext context
) {
return await context.Bar.ToListAsync();
}
How do you pass variables into a service?