[英]ASP.NET Core Web API - seeding data in .NET Core 6 using Entity Framework
In ASP.NET Core 6 Web API, I am implementing EF migration seeding.在 ASP.NET Core 6 Web API 中,我正在实施 EF 迁移播种。 When I used .NET Core 5, I applied it in
Startup.cs
and it works there.当我使用 .NET Core 5 时,我将它应用到
Startup.cs
中,它在那里工作。
public static class AppDbContextSeed
{
public static void SeedData(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
{
SeedRoles(roleManager);
SeedUsers(userManager);
}
public static void SeedRoles(RoleManager<ApplicationRole> roleManager)
{
if (!roleManager.RoleExistsAsync("Admin").Result)
{
ApplicationRole role = new ApplicationRole();
role.Name = "Admin";
IdentityResult roleResult = roleManager.
CreateAsync(role).Result;
}
if (!roleManager.RoleExistsAsync("Student").Result)
{
ApplicationRole role = new ApplicationRole();
role.Name = "Student";
IdentityResult roleResult = roleManager.
CreateAsync(role).Result;
}
}
public static void SeedUsers(UserManager<ApplicationUser> userManager)
{
if (userManager.FindByNameAsync("admin").Result == null)
{
ApplicationUser user = new ApplicationUser()
{
UserName = "admin",
Email = "admin@africa.email.com",
FirstName = "AdminFN",
LastName = "AdminLN",
NormalizedUserName = "ADMIN"
};
IdentityResult result = userManager.CreateAsync
(user, "Admin*123").Result;
if (result.Succeeded)
{
userManager.AddToRoleAsync(user, "Admin").Wait();
}
}
}
}
But now I am using .NET Core 6. So when I applied this in Program.cs
:但是现在我正在使用 .NET Core 6。所以当我在
Program.cs
中应用它时:
public void Configure(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
{
// DB Seeding
AppDbContextSeed.SeedData(userManager, roleManager);
}
I got this error:我收到此错误:
The local function 'Config' is declared but never used
声明了本地 function 'Config' 但从未使用过
Error CS0106: the modifier 'public' is not valid for this item
错误 CS0106:修饰符“public”对此项目无效
How do I correct the error and replicate this in .NET Core 6?如何更正错误并将其复制到 .NET Core 6 中?
Thank you.谢谢你。
I had to edit my last answer, the example I gave was for .net5.我不得不编辑我的最后一个答案,我给出的示例是针对 .net5 的。 Here is .net 6, I haven't tested it myself but it should work.
这是 .net 6,我自己没有测试过,但应该可以。
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
Configure(app);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
void Configure(WebApplication host)
{
using var scope = host.Services.CreateScope();
var services = scope.ServiceProvider;
try
{
var dbContext = services.GetRequiredService<MyDatabaseContext>();
if (dbContext.Database.IsSqlServer())
{
dbContext.Database.Migrate();
}
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = services.GetRequiredService<RoleManager<ApplicationRole>>();
AppDbContextSeed.SeedData(userManager, roleManager);
}
catch (Exception ex)
{
//Log some error
throw;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.