Skip to main content

Additional Context

Additional contexts allow you to communicate with more than one database. In this tutorial, you'll learn how to setup the project to account for multiple databases.

We assume

  • You have the full structure of projects ready
  • You have created a new project for each additional database context

The first step is to add the new types of the contexts to the Startup.cs file. For that, we use the property AdditionalContextsAssemblies and add the list of types of all the additional database contexts:

public class Startup : Base[...]Startup<ExceptionFilter, Guid>
{
public Startup()
{
// ...
AdditionalContextsAssemblies = new[]
{
typeof(NewDatabaseContext),
typeof(SuperBrandNewDatabaseContext)
};
}

// ...
}

Now, we can add the additional contexts in the same file, inside the method ConfigureServices:

public class Startup : Base[...]Startup<ExceptionFilter, Guid>
{
public Startup()
{
// ...
AdditionalContextsAssemblies = new[]
{
typeof(NewDatabaseContext),
typeof(SuperBrandNewDatabaseContext)
};
}

public override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);
services
.AddPersistence<DatabaseContext>()
.AddPersistenceWithSpecificContext<NewDatabaseContext>()
.AddPersistenceWithSpecificContext<SuperBrandNewDatabaseContext>()
.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new OpenApiInfo { Title = "UpDevs Demo", Version = "v1" });
});
}

// ...
}
note

You can repeat this process for all the contexts you desire to add.