2017-07-31 18 views
0

我已经创建了多个asp net核心类库。每个库都包含一个数据库上下文。根据我的项目,我已将所有dbcontexts(如Subscription,Tenant,Employee db上下文)指向一个数据库。如何使用多个dbcontext指向一个数据库

这是我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"))); 

我利用他们在我家里控制器像下面。

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

而其创建唯一的雇员表,但我需要tenants..etc也是在同一个数据库

有没有办法做到这一点我来到这里感到震惊。谁能帮我。

+0

为什么你的应用程序需要多个DbContexts?我没有看到在单个数据库上使用更多的上下文。这可能会造成比使用更多的麻烦:) –

+0

我们希望在多个项目中使用这些核心库因此,我们创建了它们分开 –

回答

0

我认为你需要来点背景下,以创建表:

DOTNET CLI命令:

dotnet ef migrations add -c TenantDbContext InitialDatabase 

dotnet ef database update -c TenantDbContext 
相关问题