예제 #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, DbDispatchingSystem context)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            // 相依性注入DbContext後,讓每次開啟應用都自動進行Migrate
            context.Database.Migrate();
            // 撒入預設的資料
            DbInit.Initialize(context);
            app.UseHttpsRedirection();
            app.UseMvc();
        }
예제 #2
0
 public static void Initialize(DbDispatchingSystem context)
 {
     // 新增資料的時候要注意,Id因為是PK,所以不用自己手動鍵入
     if(!context.Departments.Any())
     {
         var departments = new List<Department>()
         {
             new Department { Name = "資訊部", CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now },
             new Department { Name = "人資部", CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now },
             new Department { Name = "業務部", CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }
         };
         context.Departments.AddRange(departments);
         context.SaveChanges();
     }
     
     if(!context.Users.Any())
     {
         var hasher = new PasswordHasher<User>();
         var users = new List<User>()
         {
             new User 
             { 
                 Name = "哆啦a夢", 
                 Department = context.Departments.Find(1),
                 Account = "account1", 
                 Password = hasher.HashPassword(null, "password1"), 
                 CreatedAt = DateTime.Now, 
                 UpdatedAt = DateTime.Now
             },
             new User
             {
                 Name = "工藤新一",
                 Department = context.Departments.Find(2),
                 Account = "account2", 
                 Password = hasher.HashPassword(null, "password2"), 
                 CreatedAt = DateTime.Now, 
                 UpdatedAt = DateTime.Now
             }
         };
         context.Users.AddRange(users);
         context.SaveChanges();
     }
 }
예제 #3
0
 public DepartmentsController(DbDispatchingSystem _context)
 {
     context = _context;
 }