Natarajan Nagarajugari
Natarajan Nagarajugari

Reputation: 384

How to point to one database with multiple dbcontexts

I have created multiple ASP.NET Core class libraries. And each library contains one db context. As per my project, all dbcontexts (like Subscription, Tenant, Employee db context) are pointing to one database.

This is my startup.cs:

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddDbContext<Employees.EmployeeDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddDbContext<Tenants.TenantDbContext>(options =>
             options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

I am utilizing them in my home controller like below:

 EmployeeManager EmployeeManager;
    TenantManager TenantManager;
    public HomeController(EmployeeDbContext Context,TenantDbContext tenant)
    {
        EmployeeManager = new EmployeeManager(new EmployeeRepository(Context));
        TenantManager = new TenantManager(new TenantRepository(tenant));
    }

And its creating only Employee tables, but I need tenants..etc also in the same database.

Is there a way to do that?

Upvotes: 0

Views: 339

Answers (1)

Anderson Martins
Anderson Martins

Reputation: 126

I think you need to point the context to create the tables:

dotnet cli commands:

dotnet ef migrations add -c TenantDbContext InitialDatabase

dotnet ef database update -c TenantDbContext

Upvotes: 0

Related Questions