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 a third party project to your own project.
Your requirement is to add those schemas 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 GitHub here.
- BookRental
- StudentRegistration
- MultipleDbContextExample
BookRental
project contains two entities, called “Author” and “Book”.
The StudentRegistration project comprises a single entity named “Student.”MultipleDbContextExample
project contains default ASP.NET core individual authentication DbContext that will have its own entities.
In the MultipleDbContextExample, we will include the DbContexts from the 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 a high level overview of 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 project. Let’s go ahead and make modifications 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 the startup file, we will apply migration that will create tables in the database. Execute the below commands from a PowerShell console. Make sure to be in MultipleDbContextExample
project and execute the 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/BookRentalDBUpdate-Database -Context BookRentalDbContext
Add migration from StudentRegistrationDbContext
DbContext.
Add-Migration InitialStudentRegistrationDbMigration -Context StudentRegistrationDbContext -o Data/Migrations/StudentRegistration/StudentRegistrationDBUpdate-Database -Context StudentRegistrationDbContext
Once you have executed the above command, you will see tables created in the database.