-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
38 lines (27 loc) · 889 Bytes
/
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
using Automatic_migrations.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Add DbContext ao container
builder.Services.AddDbContext<ProdutoContext>(
x => x.UseSqlite("Data Source = Produtos.db")
.LogTo(Console.WriteLine, LogLevel.Information)
);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
// Migra as últimas mudanças no banco de dados durante a inicialização (migrations)
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider
.GetRequiredService<ProdutoContext>();
// Aqui a migração é executada
dbContext.Database.Migrate();
}
app.Run();