Daten in Datenbank einlesen mit Fehler bei der Kategorie beschreibung. Prüfen !
This commit is contained in:
parent
a5ce7aa496
commit
ec0a463b9b
|
@ -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 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Single_Page_Anwendung.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -13,7 +15,23 @@ namespace Aps_Single_Page_Anwendung
|
|||
{
|
||||
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) =>
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
[{
|
||||
"id": 1,
|
||||
"name": "Salate",
|
||||
"description": "Fröhlich bunte Salate"
|
||||
"beschreibung": "Fröhlich bunte Salate"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Suppen",
|
||||
"description": "Cremige Suppen"
|
||||
"beschreibung": "Cremige Suppen"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "Vorspeißen",
|
||||
"description": "Kleine Portionen"
|
||||
"beschreibung": "Kleine Portionen"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"name": "Hauptgerichte",
|
||||
"description": "Eine vielfalt aus verschiedenen Gerichten"
|
||||
"beschreibung": "Eine vielfalt aus verschiedenen Gerichten"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"name": "Nachspeißen",
|
||||
"description": "Leckere süße Desserts"
|
||||
"beschreibung": "Leckere süße Desserts"
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"name": "Getränke",
|
||||
"description": "Eine große Auswahl an Getränkeetränken"
|
||||
"beschreibung": "Eine große Auswahl an Getränkeetränken"
|
||||
}
|
||||
]
|
Loading…
Reference in New Issue
Block a user