EntityFramework.Docs icon indicating copy to clipboard operation
EntityFramework.Docs copied to clipboard

One migration set

Open Kurira opened this issue 6 years ago • 9 comments

> If you don't like having two sets of migrations, you can manually combine them into a single set that can be applied to both providers.

How to actually do this? I have two contexts MyContext and a derived MyTestingContext : MyContext with different connection string for testing, but there is no way to reuse already existing migrations for my test database. I tried using options.MigrationsAssembly("MyAssembly") but it seems that migrations are marked with DbContextAttribute which prevents reusing them.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Kurira avatar May 10 '20 15:05 Kurira

/cc @bricelam Is it possible/practical to combine migrations for two different DbContext types into a single set of migrations? (As opposed to merged migrations for multiple providers for a single DbContext.)

ajcvickers avatar May 11 '20 22:05 ajcvickers

Yes, if the model is the same between both, I believe it's possible. You'd just need two [DbContext] attributes on each migration and the snapshot.

bricelam avatar May 26 '20 21:05 bricelam

But that hasn't been tested, so you might run into issues.

bricelam avatar May 26 '20 21:05 bricelam

@Kurira Do you need two two DbContext types? Can you reuse the same type with different connection strings instead?

bricelam avatar May 26 '20 21:05 bricelam

class MyDbContext : DbContext
{
    readonly string _connectionString;

    public MyDbContext(string connectionString)
        => _connectionString = connectionString;

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlServer(_connectionString);
}
using var testContext = new MyDbContext(testConnectionString);
testContext.Database.Migrate();

bricelam avatar May 26 '20 21:05 bricelam

Two attributes doesn't compile now and I think it would clear itself on generation or would have to be added by hand on each migration. Also can't really add them as testing assembly is in different project.

Reusing the same context works as expected. But feels weird to use as it would have to be wrapped pretty hard to include all the testing functionality without polluting production context API, like

  • automatic db migration (was done on static constructor once)
  • passing connection string
  • dropping data before testing (don't really want to introduce public api on context to be accidentally used in production)
  • and even with a wrapper it would probably be unusable as you still need to pass the dbcontext type to the controller, etc.

We were using in memory provider before, so we didn't have problems up until now 😃 Not sure if this testing scenario is used widely enough to accommodate for though 😦

Kurira avatar May 27 '20 05:05 Kurira

@Kurira All your bullet points are typically things that are not directly coupled to the context. Is there any reason you want to couple them?

ajcvickers avatar May 29 '20 19:05 ajcvickers

Note, you don't have to use the string constructor. This pattern plays better with ASP.NET Core:

class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    {
    }
}
var testOptions = new DbContextOptionsBuilder<MyDbContext>()
    .UseSqlServer(testConnectionString)
    .Options;
using var testContext = new MyDbContext(testOptions);
testContext.Database.Migrate();

bricelam avatar May 29 '20 21:05 bricelam

All your bullet points are typically things that are not directly coupled to the context. Is there any reason you want to couple them?

Note, you don't have to use the string constructor. This pattern plays better with ASP.NET Core:

Our normal context is indeed using recommended context options and migrating on startup, but there is no DI and startup class in a testing project, so we prefer ease of use instead. By coupling migrate/drop data/create testing context which is easy to achieve with inheritance.

For now we went with a static helper

public static class TestContext
{
    private static DbContextOptions<MyContext> Options { get; }

    static TestContext()
    {
        Options = new DbContextOptionsBuilder<MyContext>()
            .UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=True")
            .Options;

        using MyContext context = new MyContext(Options);

        context.Database.Migrate();
    }

    public static DbContext Create()
    {
        return new MyContext(Options);
    }

    public static DbContext Drop(this DbContext context)
    {
        context.RemoveRange(context.Set<AppUser>());
        ...

        context.SaveChanges();

        return context;
    }
}

Kurira avatar May 30 '20 05:05 Kurira