Home > Enterprise >  Shifting form ASP.NET Core 3 to ASP.NET Core 6
Shifting form ASP.NET Core 3 to ASP.NET Core 6

Time:12-07

I am following a enter image description here

What the issue is and how I can solve it?

CodePudding user response:

You just need to change a little code in your project instead of changing your project structure, Please refer this simple demo.

seeder method:

public static class ContextSeed
{
    public static async Task SeedRolesAsync(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
    {
        //Seed Roles
        await roleManager.CreateAsync(new IdentityRole(Enums.Roles.SuperAdmin.ToString()));
        await roleManager.CreateAsync(new IdentityRole(Enums.Roles.Admin.ToString()));
        await roleManager.CreateAsync(new IdentityRole(Enums.Roles.Moderator.ToString()));
        await roleManager.CreateAsync(new IdentityRole(Enums.Roles.Basic.ToString()));
    }
}

Then in your program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
//.......................

var app = builder.Build();

await Configure(app);

// Configure the HTTP request pipeline.
//other middleware......
app.Run();


static async Task Configure(WebApplication host)
{
    using var scope = host.Services.CreateScope();
    var services = scope.ServiceProvider;

    try
    {
        var dbContext = services.GetRequiredService<ApplicationDbContext>();          

        var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
        var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
        await ContextSeed.SeedRolesAsync(userManager, roleManager);
    }
    catch (Exception ex)
    {
        //Log some error
        throw;
    }
}

Now, When you run your project, It will seed data into your database.

enter image description here

CodePudding user response:

Add a method

Public void Configure(IApplicationBuilder, IWebHostingEnvironment){
// your logic goes here
}

Its a mandatory for ASP apps.

  • Related