Beispiel #1
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new PostgresSQLContext(
                       serviceProvider.GetRequiredService <DbContextOptions <PostgresSQLContext> >()))
            {
                // Look for any machines. note:DB has been seeded
                if (context.Machines.Any())
                {
                    return;   // DB has been seeded
                }

                context.Machines.AddRange(
                    new Machine

                {
                    Name        = "Server 1",
                    IP          = "172.0.0.1",
                    LoginID     = "Allen",
                    Password    = "******",
                    OS          = "Windos",
                    HostIP      = "192.168.0.1",
                    Description = "Server 1 Desc",
                },

                    new Machine
                {
                    Name        = "Server 2",
                    IP          = "172.0.0.2",
                    LoginID     = "Rong",
                    Password    = "******",
                    OS          = "Linux",
                    HostIP      = "192.168.0.1",
                    Description = "Server 2 Desc",
                }
                    );

                //insert to DB
                context.SaveChanges();
            }
        }
 public PermissionController(PostgresSQLContext context, ILogger <PermissionController> logger) : base(context, logger)
 {
     _context = context;
     _logger  = logger;
 }
 public BaseController(PostgresSQLContext context, ILogger <Controller> logger)
 {
     _context = context;
     _logger  = logger;
 }
 public AccountController(PostgresSQLContext context, IAuthenticationService authService, ILogger <AccountController> logger) : base(context, logger)
 {
     _authService = authService;
     _context     = context;
     _logger      = logger;
 }
 //建構函式會使用[相依性插入]將資料庫內容(PostgresSQLContext) 插入到控制器中。
 //控制器中的每一個 CRUD 方法都會使用資料庫內容。
 public HomeController(PostgresSQLContext context, ILogger <HomeController> logger) : base(context, logger)
 {
     _context = context;
     _logger  = logger;
 }