Skip to content

Add EntityFramework Migrations Support #89

@DavidNorena

Description

@DavidNorena

Because of #72 and #88 and many others that are there and will come, I think is a good way to go and bring this feature to life together.

So after a research I found this dotnet/efcore#9033 (comment) for DbContext discovery:

We always look in three places:

  1. Referenced by IDesignTimeDbContextFactory implementations in the assembly or startup assembly
  2. Registered in the application's service provider
  3. DbContext derived types in the assembly or startup assembly

After we've discovered them all, we pick one to use.

I've considered having a way to bypass the type discovery (e.g. specify an assembly-qualified name on the command) and then discover a construction mechanism (factory first, service provider second, default constructor last), but this would require a completely separate (new) code path, and the value always seemed minimal.

From my point of view we can go for IDesignTimeDbContextFactory Implementation (because we have a "Virtual DbContext"), so we can override the construction of the DbContext and provide the IStorage.StorageContext that is derived from DbContext.

Something like this:

using ExtCore.Data.EntityFramework.PostgreSql;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace RadarJudicial.Data.App.Core
{
  public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<StorageContext>
  {
    public StorageContext CreateDbContext(string[] args)
    {
      // *** code for example purposes ***
      //var builder = new DbContextOptionsBuilder<StorageContext>();
      //builder.UseNpgsql(
          //"Host=localhost;Port=5432;User ID=radar;Password=radar123*;Database=radar;Pooling=true;");
      // *** code for example purposes ***
      
      // Here we need to Find resolve IStorage
      // Then find all Implementations of RepositoryBase
      // and/or seed every DbSet of the IStorage.StorageContext
      
      //Then return IStorage.StorageContext

      // Only for example purposes
      //return IStorage.StorageContext();
    }
  }
}

The bad news is that we cannot resolve services from here, and also this PoC is "attached" to PostgreSql, so we need a make a generic version of it .... what do you think @DmitrySikorsky does this give you more or less my idea ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions