【问题标题】:How to point to one database with multiple dbcontexts如何指向一个具有多个 dbcontexts 的数据库
【发布时间】:2018-01-07 09:26:34
【问题描述】:

我创建了多个 ASP.NET Core 类库。每个库都包含一个数据库上下文。根据我的项目,所有 dbcontexts(如订阅、租户、员工 db 上下文)都指向一个数据库。

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

它只创建 Employee 表,但我需要租户..etc 也在同一个数据库中。

有没有办法做到这一点?

【问题讨论】:

  • 为什么您的应用程序需要多个 DbContext?我认为在单个数据库上拥有更多上下文没有用。这可能会比使用造成更多的麻烦:)
  • 我们想在多个项目中使用这些核心库所以我们分开创建它们

标签: razor entity-framework-core asp.net-core-1.1


【解决方案1】:

我认为您需要指出上下文来创建表格:

dotnet cli 命令:

dotnet ef migrations add -c TenantDbContext InitialDatabase

dotnet ef database update -c TenantDbContext

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 2017-07-07
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多