나는이 방법으로 이것을 달성했다. DBContext 클래스의 확장 클래스에 시드 메서드를 만들고 시작 클래스의 configure 메서드에서이 메서드를 호출했습니다.
public static class CrmContextExtension
{
public static async Task SeedDataForContext(this CrmContext context)
{
// if there is no role exist then create some default role 'administrator'
var logger = LogManager.GetCurrentClassLogger();
if (context.Roles.Count() == 0)
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store,null, null, null, null,null);
var role = new IdentityRole { Name = "administrator" };
IdentityResult result = await manager.CreateAsync(role);
logger.Info(result.Succeeded ? "Role 'administrator' has been successfully created" :
"Couldn't create role 'administrator' in the seed.");
}
}
}
public async void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory, CrmContext context)
{ ......
await context.SeedDataForContext();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
문제가 발생하면 알려주세요.
당신이 찾고있는 것이 명확하지 않습니다. 아마도 [this] (https://github.com/aspnet/Entropy/blob/release/samples/MusicStore.Spa/Models/SampleData.cs)가 도움이 될 수 있습니다. – tmg
@tmg 그것은 내가 필요한 것입니다! –