How to control migrations in two distinct contexts?

Asked

Viewed 84 times

4

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

For example

Add-Migration Contexto 1, 
Add-Migration 2

How can I add new ones Migrations for each context?

1 answer

4


You must add the parameter -Context in charge Add-Migrations

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

In the example I set up 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 the DbContextOptions.

I had to add in ConfigureServices class Startup.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.