-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
66 lines (51 loc) · 1.98 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 微軟範例
// .Net 6 MVC EF Core
// https://docs.microsoft.com/zh-tw/aspnet/core/data/ef-mvc/?view=aspnetcore-6.0
using ContosoUniversityNet6.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
//// 資料庫連線
//builder.Services.AddDbContext<SchoolContext>(options => {
// options.UseSqlServer(builder.Configuration.GetConnectionString("SchoolContext"))
// //啟用 Logging 觀察 SQL 指令
// .UseLoggerFactory(LoggerFactory.Create(builder => {
// builder.AddConsole().AddDebug();
// }));
//});
////資料庫連線﹐doker 改用以下語法﹐將appsettings值和任何環境變數值配對
builder.Services.AddDbContext<SchoolContext>(options => {
options.UseSqlServer(builder.Configuration["ConnectionStrings:SchoolContext"])
.UseLoggerFactory(LoggerFactory.Create(builder => {
builder.AddConsole().AddDebug();
}));
});
// 資料庫例外狀況篩選
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) {
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
} else {
// 資料庫例外狀況篩選
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
////建立資料庫(在開發環境中則會比較方便在一開始執行時自動建立一個資料庫並建立初始的資料, 不適用於正式環境)
//using (var scope = app.Services.CreateScope()) {
// var services = scope.ServiceProvider;
// var context = services.GetRequiredService<SchoolContext>();
// context.Database.EnsureCreated();
// DbInitializer.Initialize(context); //建立初始資料
//}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();