Question:
I have two contexts and I would like to know how do I activate each of them.
For example
Add-Migration Contexto 1,
Add-Migration 2
How can I add new migrations for each context?
Answer:
You must add the -Context
parameter in the Add-Migrations
command
Add-Migration Teste -Context:DoisContext.Data.OutroContext
In the example I set up my Contexts are as follows
public class OutroContext : DbContext
{
public DbSet<Teste> Testes { get; set; }
public OutroContext(DbContextOptions<OutroContext> options)
: base(options)
{
}
}
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);
}
}
Important detail for your constructors, where both are getting the DbContextOptions
.
Had to also add in ConfigureServices
of Startup
class.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<OutroContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));