Test Entity Framework

This commit is contained in:
Simon Lübeß
2025-05-27 14:17:47 +02:00
parent dffd31cd0f
commit 9e7ec186cd
8 changed files with 314 additions and 0 deletions

View File

@ -3,6 +3,7 @@ using System.Text;
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using USEntryCoach.Server.Data;
using USEntryCoach.Server.Services;
@ -53,6 +54,9 @@ builder.Services.AddAuthorization(options =>
});
});
builder.Services.AddDbContext<BloggingContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
var app = builder.Build();
app.UseAuthentication();
@ -176,4 +180,29 @@ app.MapGet("/ephemeral_token", async () =>
app.MapFallbackToFile("/index.html");
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<BloggingContext>();
context.Database.EnsureCreated();
Blog b = new()
{
Url = "BLa"
};
Post p = new()
{
Blog = b,
Content = "sd zgsödiog söffdgkjasödfl kjasdfökljasdfölasddj föadj kfö",
Title = "Hallo Welt"
};
b.Posts.Add(p);
context.Blogs.Add(b);
context.SaveChanges();
}
app.Run();