3

Is it possible to support two different EF Core models with all "migrations functionality" in one database?

I guess I should somehow separate dbo._EFMigrationsHistory ? Can't find such option in documentation.

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
  • 1
    I don't have a detailed example, but one example is the [IdentityServer.Samples](https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts/Combined_AspNetIdentity_and_EntityFrameworkStorage) repo (CombinedAspIdentity project). That project uses three contexts, with three sets of migrations. The migrations can be specified like so for each context (from the cli, not pmc): `dotnet ef migrations add InitialIdentityServerConfigurationDbMigration --context ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb` – Kevin Fichter Jun 29 '18 at 14:57
  • Thank you. From first look.. I can't understand how it works. Why clients config files doesn't contain connection strings? Maybe you know where to find them? From them should be clear how many db they uses (because it is not obvious that only one and the same). – Roman Pokrovskij Jun 29 '18 at 20:42
  • There's just one connection string (three contexts in the same db). The connecting string is in AppSettings.json – Kevin Fichter Jun 30 '18 at 01:23
  • Some related documentation links: [Custom Migrations History Table](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/history-table) and [Migrations with Multiple Providers](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/providers) – Ivan Stoev Jun 30 '18 at 09:51
  • @K_foxer9 : here is the appsetting.json : https://github.com/IdentityServer/IdentityServer4.Samples/blob/release/Quickstarts/Combined_AspNetIdentity_and_EntityFrameworkStorage/src/MvcClient/appsettings.json . no connection string – Roman Pokrovskij Jun 30 '18 at 10:32
  • Sorry for not being more specific; this is a big repo with several solutions containing several projects each. [Here's my fork](https://github.com/kevdever/IdentityServer4.Samples/blob/UseSqlServer/Quickstarts/Combined_AspNetIdentity_and_EntityFrameworkStorage/src/IdentityServerWithAspIdAndEF_2_1/appsettings.json). The solution is contained within Quickstarts/Combined..../src; the project of note is /IdentityServerWithAspIdAndEF_2_1. You'll see that appsettings.json has two conn strings (one commented out), supporting SqlLite and SqlServer. The Data folder holds three migrations... – Kevin Fichter Jun 30 '18 at 17:07

1 Answers1

3

Yes, you can have multiple contexts within the same database. I've put together a sample showing three contexts in the same database. You declare three contexts as you usually would, and the differences manifest themselves when you setup migrations; you just need to be more explicit here, naming the context you're migrating or updating, and it is a good idea to also be explicit about where the migration is going.

Here's the Program.Main of the sample project linked above, showing a basic addition of three contexts for dependency injection, then doing "stuff" with each. See the repo on Github for the rest of the files.

    class Program
{
    static async Task Main(string[] args)
    {
        var providers = new ServiceCollection()
            .AddDbContext<AnimalContext>(options => options.UseSqlServer(Globals.CONNECTIONSTRING))
            .AddDbContext<DoctorContext>(options => options.UseSqlServer(Globals.CONNECTIONSTRING))
            .AddDbContext<CarContext>(options => options.UseSqlServer(Globals.CONNECTIONSTRING))
            .BuildServiceProvider();

        var animalContext = providers.GetService<AnimalContext>();
        await AnimalHelpers.Seed(animalContext);
        await AnimalHelpers.Read(animalContext);

        var carContext = providers.GetService<CarContext>();
        await CarHelpers.Seed(carContext);
        await CarHelpers.Read(carContext);

        var doctorContext = providers.GetService<DoctorContext>();
        await DoctorHelpers.Seed(doctorContext);
        await DoctorHelpers.Read(doctorContext);

        Console.ReadKey();

    }
}

To initialize the database, I ran a migration for each context, then ran update database for each, as follows (in powershell; PMC commands/flags differ slightly):

Migration:

dotnet ef migrations add CreateAnimalsSchema --context AnimalContext -o Data/Migrations/Animals

Database Update:

dotnet ef database update --context AnimalContext

Kevin Fichter
  • 1,067
  • 1
  • 11
  • 17
  • I will try it. "just do it as always". but I still do not understand what happens witgh dbo.__EFMigrationsHistory ... how it can register multiplecotnexts if contains only MigrationId and version number columns... – Roman Pokrovskij Jul 02 '18 at 08:44
  • 1
    Fixed an errant '#' in the migration command. Regarding MigrationsHistory, once you have initialized them, new migrations are placed in the proper folder automagically, as long as you specify the context each time. As far as the database is concerned, I don't see why it would care about EF contexts; it just keeps track of migrations by id, and EF core keeps track of which migration goes with which context. For the sample project, its dbo__MigrationsHistory has three entries: `20180630184548_CreateDoctorSchema`, `20180630184557_CreateAnimalsSchema`, `20180630184610_CreateCarsSchema` – Kevin Fichter Jul 02 '18 at 14:09