Asp.Net_Single_Page_App/Single_Page_Anwendung/Models/DatenEinlesen.cs

43 lines
1.3 KiB
C#
Raw Normal View History

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();
}
}
}
}