我如何 运行 迁移(第一次迁移)具有 Asp.net 核心身份和最小 API .Net 6

How can I run migration (first migration) with Asp.net core Identity with Minimal API .Net 6

我创建了一个 asp.net 6 最小 API 项目并按如下方式构建它:

Program.cs

using WebApplication1.EndPointExtension;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointDefinitions(typeof(IEndpointDefinition));


var app = builder.Build();
app.UseEndpointDefinitions();
if (builder.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

IEndpointDefinition.cs

public interface IEndpointDefinition
{
    void DefineServices(IServiceCollection services);
    void DefineEndpoints(WebApplication app);
}

EndpointDefinition.cs

public static class EndpointDefinitionExtensions
{
    public static void AddEndpointDefinitions(
        this IServiceCollection services, params Type[] scanMarkers)
    {
        var endpointDefinitions = new List<IEndpointDefinition>();

        foreach (var marker in scanMarkers)
        {
                endpointDefinitions.AddRange(
                marker.Assembly.ExportedTypes
                .Where(x => typeof(IEndpointDefinition).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
                .Select(Activator.CreateInstance).Cast<IEndpointDefinition>());
        }

        foreach (var endpointDefinition in endpointDefinitions)
        {
            endpointDefinition.DefineServices(services);
        }

        services.AddSingleton(endpointDefinitions as IReadOnlyCollection<IEndpointDefinition>);
    }

    public static void UseEndpointDefinitions(this WebApplication app)
    {
        var definitions = app.Services.GetRequiredService<IReadOnlyCollection<IEndpointDefinition>>();

        foreach (var endpointDefinition in definitions)
        {
            endpointDefinition.DefineEndpoints(app);
        }
    }
}

下面是我的数据库上下文 class,它实现了端点接口并继承了核心标识 class DatabaseDBContext.cs

public class DatabaseDBContext : IdentityDbContext, IEndpointDefinition
{
    public DatabaseDBContext(DbContextOptions<DatabaseDBContext> options) : base(options)
    {

    }

    public void DefineEndpoints(WebApplication app)
    {
        app.UseAuthentication();
    }

    public void DefineServices(IServiceCollection services)
    {
        services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<DatabaseDBContext>();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

        var connectionString = configuration.GetConnectionString("AppDb");
        if (!optionsBuilder.IsConfigured)
            optionsBuilder.UseSqlServer(connectionString);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

我已经安装了迁移所需的 NuGet 包并在 appsetting.js 中提供了连接字符串,但是当我构建项目时它显示错误

System.MissingMethodException: 'Cannot dynamically create an instance of type 'WebApplication1.DatabaseDBContext'. Reason: No parameterless constructor defined

我想知道到底哪里出了问题,或者我没有使用正确的方法。

经过多次尝试解决它,我只是明确地调用了默认构造函数

public DatabaseDBContext()
{

}

成功了。