使用EFCore中的单个数据库处理多个DBContext的通用标识和迁移

时间:2018-11-12 07:12:36

标签: entity-framework-core

项目三中,我有 ProjectOneDBContext ProjectTwoDBContext 指向两个网络的相同数据库应用程序在一起。我在处理相同的迁移程序集并具有通用身份表

时遇到问题

ProjectOne的Startup.cs 中:

services.AddDbContext<ProjectOneDBContext>(options =>
        options.UseSqlServer(
            databaseConnectionString,
            o => o.MigrationsAssembly(projectThreeMigrationAssembly)),
        ServiceLifetime.Singleton,
        ServiceLifetime.Singleton);

ProjectTwo的Startup.cs 中:

services.AddDbContext<ProjectTwoDBContext>(options =>
        options.UseSqlServer(
            databaseConnectionString,
            o => o.MigrationsAssembly(projectThreeMigrationAssembly)),
        ServiceLifetime.Singleton,
        ServiceLifetime.Singleton);

ProjectThree中的ProjectOneDBContext.cs

public class ProjectOneDBContext : IdentityDbContext<ApplicationUser>
{
...
  public void ConfigureDevelopment(...)
  {
   ...
   context.Database.Migrate();
   ...
  }
...
}

ProjectThree中的ProjectTwoDBContext.cs

public class ProjectTwoDBContext : IdentityDbContext<ApplicationUser>
    {
    ...
      public void ConfigureDevelopment(...)
      {
       ...
       context.Database.Migrate();
       ...
      }
    ...
    }

1 个答案:

答案 0 :(得分:0)

ProjectTwoDBContext不必从IdentityDbContext继承,因为ProjectOneDBContext已经继承了它。