如何更改迁移生成器 EF CORE 2.x?

How to change migration generator EF CORE 2.x?

我需要在迁移生成器中添加一些功能,并在迁移中自动添加 InsertData。

我已经创建了自定义 CSharpMigrationsGenerator,但我不知道如何以及在何处注册它。

public class CustomCSharpMigrationCodeGenerator : CSharpMigrationsGenerator
    {
        public override string GenerateMigration(string migrationNamespace, 
            string migrationName, 
            IReadOnlyList<MigrationOperation> upOperations, 
            IReadOnlyList<MigrationOperation> downOperations)
        {
            throw new Exception("Hello world");
            return base.GenerateMigration(migrationNamespace, migrationName, upOperations, downOperations);
        }

        public CustomCSharpMigrationCodeGenerator(MigrationsCodeGeneratorDependencies dependencies, 
            CSharpMigrationsGeneratorDependencies csharpDependencies) : base(dependencies, csharpDependencies)
        {
            throw new Exception("Hello world");
        }
    }

我试过了,但是没用

public class MyDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddSingleton<CSharpMigrationsGenerator, CustomCSharpMigrationCodeGenerator>();
    }
}

我需要在哪里注册呢?还是我用错了class?

你快到了。 class 实现的服务(您需要更换)是 IMigrationsCodeGenerator:

public class MyDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddSingleton<IMigrationsCodeGenerator, CustomCSharpMigrationCodeGenerator>();
    }
}