50-Identity MVC:DbContextSeed初始化

1-创建一个可以启动时如果没有一个账号刚创建1个新的账号

namespace MvcCookieAuthSample.Data
{
    public class ApplicationDbContextSeed
    {
        public async Task SeedAsync(ApplicationDbContext context, IServiceProvider service)
        {
            if (!context.Users.Any())
            {
                var _userManager= service.GetRequiredService<UserManager<ApplicationUser>>();
                var user = new ApplicationUser() {
                     Email="qinzhb@163.com",
                     UserName="qinzhb",
                     NormalizedUserName="qinzhb"
                };

                var result = await _userManager.CreateAsync(user, "123456@Byd");
                if (!result.Succeeded)
                {
                    throw new Exception("初始化用户失败");
                }

            }
        }
    }
}

 

2-增加一个扩展方法,用于启动时初始化数据库

namespace MvcCookieAuthSample
{
    public static class WebHostMigrationExtensions
    {
        public static IWebHost MigrationDbContext<TContext>(this IWebHost host,Action<TContext ,IServiceProvider> seeder)
            where TContext:DbContext
        {
            using (var scope = host.Services.CreateScope())
            {
                var service = scope.ServiceProvider;
                var logger = service.GetRequiredService<ILogger<TContext>>();
                var context =  service.GetService<TContext>();

                try
                {
                    context.Database.Migrate();
                    seeder(context, service);
                    logger.LogInformation("执行方法数据库初始化成功");

                }
                catch
                {
                    logger.LogError("执行数据库数据库初始化密码失败");
                }

            }
            
            return host;
        }
    }
}

3-把上面的2方法增加到启动类中

  public static void Main(string[] args)
        {
            CreateWebHostBuilder(args)
                
                .Build()
                .MigrationDbContext<ApplicationDbContext>((context,services)=> {
                    new Data.ApplicationDbContextSeed().SeedAsync(context, services).Wait();
                }).Run();
        }

 

转载于:https://www.cnblogs.com/qinzb/p/9458105.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
File "/usr/lib/python2.7/site-packages/keystoneauth1/adapter.py", line 282, in get_endpoint 2023-05-22 12:11:00.079 10244 ERROR nova return self.session.get_endpoint(auth or self.auth, **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 1218, in get_endpoint 2023-05-22 12:11:00.079 10244 ERROR nova return auth.get_endpoint(self, **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/base.py", line 380, in get_endpoint 2023-05-22 12:11:00.079 10244 ERROR nova allow_version_hack=allow_version_hack, **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/base.py", line 271, in get_endpoint_data 2023-05-22 12:11:00.079 10244 ERROR nova service_catalog = self.get_access(session).service_catalog 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/base.py", line 134, in get_access 2023-05-22 12:11:00.079 10244 ERROR nova self.auth_ref = self.get_auth_ref(session) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/generic/base.py", line 208, in get_auth_ref 2023-05-22 12:11:00.079 10244 ERROR nova return self._plugin.get_auth_ref(session, **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/v3/base.py", line 188, in get_auth_ref 2023-05-22 12:11:00.079 10244 ERROR nova authenticated=False, log=False, **rkwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 1124, in post 2023-05-22 12:11:00.079 10244 ERROR nova return self.request(url, 'POST', **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 961, in request 2023-05-22 12:11:00.079 10244 ERROR nova raise exceptions.from_response(resp, method, url) 2023-05-22 12:11:00.079 10244 ERROR nova Unauthorized: The request you have made requires authentication. (HTTP 401) (Request-ID: req-009eabd5-6dd8-42fe-80ea-2fc398
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值