Daten in Datenbank einlesen mit Fehler bei der Kategorie beschreibung. Prüfen !

This commit is contained in:
marcusferl@weifer.de 2022-06-08 08:53:44 +02:00
parent a5ce7aa496
commit ec0a463b9b
3 changed files with 62 additions and 8 deletions

View File

@ -1,6 +1,42 @@
namespace Single_Page_Anwendung.Models using Aps_Single_Page_Anwendung.Models;
using Aps_Single_Page_Anwendung.Repositories;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
namespace Single_Page_Anwendung.Models
{ {
public class DatenEinlesen public class DatenEinlesen
{ {
public static void Initialize(IServiceProvider serviceProvider)
{
// Holy shit Oo
using (var futterContext = new FutterContext(serviceProvider.GetRequiredService<DbContextOptions<FutterContext>>()))
{
if (futterContext.Speisen.Any())
{
return;
}
var kategorieRepository =
new FileKategorieRepository(serviceProvider.GetRequiredService<IWebHostEnvironment>());
var kategorien = kategorieRepository.GetKategorien().ToList();
kategorien.ForEach(k =>
{
k.Id = 0;
foreach (var speise in k.Speisen)
{
speise.Id = 0;
speise.KategorieId = 0;
}
});
futterContext.Kategorien.AddRange(kategorien);
futterContext.SaveChanges();
}
}
} }
} }

View File

@ -1,7 +1,9 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Single_Page_Anwendung.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -13,7 +15,23 @@ namespace Aps_Single_Page_Anwendung
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
CreateHostBuilder(args).Build().Run(); var host = CreateHostBuilder(args).Build();
using(var scope = host.Services.CreateScope())
{
var service = scope.ServiceProvider;
try
{
DatenEinlesen.Initialize(service);
}
catch (Exception ex)
{
var logger = service.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Fehler mit der Datenbank!");
throw;
}
}
host.Run();
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>

View File

@ -1,31 +1,31 @@
[{ [{
"id": 1, "id": 1,
"name": "Salate", "name": "Salate",
"description": "Fröhlich bunte Salate" "beschreibung": "Fröhlich bunte Salate"
}, },
{ {
"id": 2, "id": 2,
"name": "Suppen", "name": "Suppen",
"description": "Cremige Suppen" "beschreibung": "Cremige Suppen"
}, },
{ {
"id": 3, "id": 3,
"name": "Vorspeißen", "name": "Vorspeißen",
"description": "Kleine Portionen" "beschreibung": "Kleine Portionen"
}, },
{ {
"id": 4, "id": 4,
"name": "Hauptgerichte", "name": "Hauptgerichte",
"description": "Eine vielfalt aus verschiedenen Gerichten" "beschreibung": "Eine vielfalt aus verschiedenen Gerichten"
}, },
{ {
"id": 5, "id": 5,
"name": "Nachspeißen", "name": "Nachspeißen",
"description": "Leckere süße Desserts" "beschreibung": "Leckere süße Desserts"
}, },
{ {
"id": 6, "id": 6,
"name": "Getränke", "name": "Getränke",
"description": "Eine große Auswahl an Getränkeetränken" "beschreibung": "Eine große Auswahl an Getränkeetränken"
} }
] ]