Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A relational store has been configured without specifying either the DbConnection or connection string to use. #769

Open
DesertMountSolution opened this issue Nov 15, 2023 · 2 comments
Labels

Comments

@DesertMountSolution
Copy link

Can not connect to database.

Register

services.AddMultiTenant<TenantInfo>()
    .WithEFCoreStore<MultiTenantStoreDbContext, TenantInfo>()
    .WithRouteStrategy()
    .WithRedirectStrategy("/notenant/index");

EfContext

public class MultiTenantStoreDbContext : EFCoreStoreDbContext<TenantInfo>
{
    private readonly IConfiguration _configuration;


    public MultiTenantStoreDbContext(DbContextOptions<MultiTenantStoreDbContext> options,
        IConfiguration configuration) : base(options)
    {
        _configuration = configuration;
    }


    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connectionString = _configuration.GetConnectionString("MultiTenant");

        var serverVersion = ServerVersion.AutoDetect(connectionString);

        optionsBuilder.UseMySql(serverVersion);

        base.OnConfiguring(optionsBuilder);
    }
}

Try to resolve tenant store

using var scope = app.ApplicationServices.CreateScope();
var store = scope.ServiceProvider.GetRequiredService<IMultiTenantStore<TenantInfo>>();            

var tenants = await store.GetAllAsync();

GetAllAsync throws an exception with "A relational store has been configured without specifying either the DbConnection or connection string to use."

@DesertMountSolution
Copy link
Author

I need to set connection string in constructor.

public MultiTenantStoreDbContext(DbContextOptions<MultiTenantStoreDbContext> options,
    IConfiguration configuration) : base(options)
{
    _configuration = configuration;

    var connectionString = _configuration.GetConnectionString("MultiTenant");

    Database.SetConnectionString(connectionString);
}

@AndrewTriesToCode
Copy link
Sponsor Contributor

hi @DesertMountSolution

I'm not familiar with using server version and autodetect like you have here:

var connectionString = _configuration.GetConnectionString("MultiTenant");
var serverVersion = ServerVersion.AutoDetect(connectionString); 
optionsBuilder.UseMySql(serverVersion);

Does UseMySql not just accept the connection string as normal?

There's nothing special about EFCoreStoreDbContext<TenantInfo> in how it connects to a database. The usual approach of assigning the provider and connection string wither in the service setup or in OnConfiguring should work just like any other db context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants