Use AddDBContext registered service inside the CustomStore #635
KarthikEK20
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I’m following .NET Core and I'm trying to use Custom Store by implementing the IMultiTenantStore interface. Here I need to use DBContext instance, so I'm trying to use the service that registered for DBContext via AddDBContext in program.cs. But there is no direct way inject the DBConext Dependency Injection via the Custom Store class construction or I don't know the way to achieve this.
The purpose is, I have kept all my tenant information in the database tables. I need to query a few tables in my DB to collect my tenant store data. So, I couldn't use EF Core Store the direct way you offered and prefer to go Custom Store.
`builder.Services.AddDbContextPool(options =>
{
options.UseNpgsql("ConnectionString",
npgsqlOptionsAction => npgsqlOptionsAction.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(15), errorCodesToAdd: null));
});
// MultiTenant Service
builder.Services.AddMultiTenant()
.WithHeaderStrategy("tenant")
.WithStore(ServiceLifetime.Singleton, obj => new MyTenantStore());`
Note: I can use DBContext that initialized in OnConfiguring(DbContextOptionsBuilder optionsBuilder) by just creating new DBContext instance inside the CustomStore class. But i dont want this way, I want to follow the Dependency Injection and AddDbContext service option offered by core.
Please guide me how to make it possible. Or Anyother alternative ways to do it.
Beta Was this translation helpful? Give feedback.
All reactions