EF database update in Azure functions project with migrations in class library

Viewed 58

I have an Azure functions project that uses EF Core. When I keep the DbContext and the Migrations in the same project, everything works fine as long as there is an implementation of IDesignTimeContextFactory:

public class MyReportContextFactory : IDesignTimeDbContextFactory<MyReportContext>
{
    public MyReportContext CreateDbContext(string[] args)
    {
        IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("local.settings.json")
            .Build();

        var optionsBuilder = new DbContextOptionsBuilder<MyReportContext>();
        optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));

        return new MyReportContext(optionsBuilder.Options);
    }
}

If I now create a new class library project, called Persistance, and move MyReportContext into it, I am still able to generate the migration files as long as I specify the startup project and the project:

dotnet ef migrations add InitialCreate --startup-project .\MyReportGenerator\ --project .\Persistance\

The problem is if I try to run the database update command

dotnet ef database update --verbose

C:...\Persistance\Migrations\20220913122443_InitialCreate.cs(1,37): error CS0234: The type or namespace name 'Migrations' does not exist in the namespace 'Microsoft.EntityFrameworkCore' (are you missing an assembly reference?) [C:...\Persistance\Persistance.csproj] C:...\Persistance\Migrations\20220913122443_InitialCreate.Designer.cs(6,37): error CS0234: The type or namespace name 'Migrations' does not exist in the namespace 'Microsoft.EntityFrameworkCore' (are you missing an assembly reference?) [C:...\Persistance\Persistance.csproj] C:...\Persistance\Migrations\20220913122443_InitialCreate.cs(7,42): error CS0246: The type or namespace name 'Migration' could not be found (are you missing a using directive or an assembly reference?) [C:...\Persistance\Persistance.csproj] C:...\Persistance\Migrations\RigReportContextModelSnapshot.cs(13,51): error CS0246: The type or namespace name 'ModelSnapshot' could not be found (are you missing a using directive or an assembly reference?) [C:...\Persistance\Persistance.csproj] C:...\Persistance\Migrations\20220913122443_InitialCreate.Designer.cs(14,6): error CS0246: The type or namespace name 'MigrationAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:...\Persistance\Persistance.csproj] C:...\Persistance\Migrations\20220913122443_InitialCreate.Designer.cs(14,6): error CS0246: The type or namespace name 'Migration' could not be found (are you missing a using directive or an assembly reference?) [C:...\Persistance\Persistance.csproj] C:...\Persistance\Migrations\20220913122443_InitialCreate.cs(9,36): error CS0246: The type or namespace name 'MigrationBuilder' could not be found (are you missing a using directive or an assembly reference?) [C:...\Persistance\Persistance.csproj] C:...\Persistance\Migrations\20220913122443_InitialCreate.cs(88,38): error CS0246: The type or namespace name 'MigrationBuilder' could not be found (are you missing a using directive or an assembly reference?) [C:...\Persistance\Persistance.csproj]

0 Answers
Related