Code First Multiple DB Context Migration

In Entity Framework Core 2.0, you can implement multiple DbContext and apply changes to one single database by using multiple projects. For instance, you are planning to add schema from third party project to your own project.
Your requirement is to add those schema to your single database. It can be any database; for example SqlLite, SQLServer, or MySQL.
In this tutorial, I will be using SQLServer with EntityFramework Core 2.0. Below are 3 projects with their own DbContext. You can find this project on my GitHub here.
- BookRental
- StudentRegistration
- MultipleDbContextExample
BookRental
project contains two entities called "Author" and "Book".
StudentRegistration
project contains one entity called "Student".
MultipleDbContextExample
project contains default ASP.NET core individual authentication DbContext that will have its own entities.
In MultipleDbContextExample
, we will add DbContext from BookRental
and StudentRegistration
projects. That will add all the entities to one single database.
Here are DbContexts from individual projects. I would assume that you already know how to create models, entities, etc. This tutorial will give you high level overview on how to apply migrations to one single SQL database.
BookRental project DbContext
namespace BookRental.Models
{
public class BookRentalDbContext : DbContext
{
public BookRentalDbContext (DbContextOptions<BookRentalDbContext> options)
: base(options)
{
}
public DbSet<BookRental.Models.Author> Author { get; set; }
public DbSet<BookRental.Models.Book> Book { get; set; }
}
}
StudentRegistration project DbContext
namespace StudentRegistration.Models
{
public class StudentRegistrationDbContext : DbContext
{
public StudentRegistrationDbContext(DbContextOptions<StudentRegistrationDbContext> options)
: base(options)
{
}
public DbSet<StudentRegistration.Models.Student> Student { get; set; }
}
}
MultipleDbContextExample project DbContext
namespace MultipleDbContextExample.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
}
Now we have DbContext from each projects. Let's go ahead and make modification to startup.cs
file in MultipleDbContextExample
project that will maintain one single instance of SQL database.
In ConfigureServices
DI, I have defined two DbContext for BookRentalDbContext
and StudentRegistrationDbContext
.
public void ConfigureServices(IServiceCollection services)
{
// Default ASP.NET Authentication DbContext
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// BookRentalDbContext from BookRental project
services.AddDbContext<BookRentalDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), o => o.MigrationsAssembly("MultipleDbContextExample"))
);
// StudentregistrationDbContext from StudentRegistration project
services.AddDbContext<StudentRegistrationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), o => o.MigrationsAssembly("MultipleDbContextExample"))
);
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
}
Once you have defined your own DbContext in startup file we will apply migration that will create tables in database. Execute below commands from PowerShell console. Make sure to be in MultipleDbContextExample
project and execute below commands.
Default Db migration from MultipleDbContextExample
project.
Update-Database -Context ApplicationDbContext
Add migration from BookRentalDbContext
DbContext.
Add-Migration InitialBookRentalDbMigration -Context BookRentalDbContext -o Data/Migrations/BookRental/BookRentalDB
Update-Database -Context BookRentalDbContext
Add migration from StudentRegistrationDbContext
DbContext.
Add-Migration InitialStudentRegistrationDbMigration -Context StudentRegistrationDbContext -o Data/Migrations/StudentRegistration/StudentRegistrationDB
Update-Database -Context StudentRegistrationDbContext
Once you have executed above command, you will see tables created in database.