How to control migrations two different contexts?

3

I have two contexts and would like to know how to activate each of them.

For example

Add-Migration Contexto 1, 
Add-Migration 2

How can I add new migrations for each context?

    
asked by anonymous 10.01.2018 / 09:55

1 answer

3

You should add the parameter -Context to the command Add-Migrations

Add-Migration Teste -Context:DoisContext.Data.OutroContext

In the example that I mounted my Context 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 builders, where both are getting DbContextOptions .

I also had to add in ConfigureServices of class Startup .

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

services.AddDbContext<OutroContext>(options =>
    options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
    
10.01.2018 / 11:36