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

Time:11-25

I have created an asp.net 6 minimal API project and structured it as follows:

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);
        }
    }
}

Now following is my DB Context class which implements the endpoint interface and inherit core identity 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);
    }
}

I have installed NuGet packages required for migration and provided connection string in appsetting.js but when I build the project it shows the error as

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

I want to know where exactly things are going wrong or I am not using the right approach.

CodePudding user response:

After so many times trying to resolve it, I just called the default constructor explicitly

public DatabaseDBContext()
{

}

and it worked.

  • Related