Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace TrainAPIDb
- {
- public static class DbSeederExtension
- {
- public static void Seed (this ModelBuilder modelBuilder)
- {
- SeedTypes(modelBuilder);
- SeedOwners(modelBuilder);
- SeedTrains(modelBuilder);
- SeedEmployees(modelBuilder);
- SeedReperations(modelBuilder);
- }
- private static void SeedReperations(ModelBuilder modelBuilder)
- {
- modelBuilder.Entity<Reparation>().HasData(new Reparation
- {
- Id = 1,
- TrainId = 1,
- EmployeeId = 37,
- From = new DateTime(2008,9,14),
- To = new DateTime(2009,11,2)
- });
- modelBuilder.Entity<Reparation>().HasData(new Reparation
- {
- Id = 2,
- TrainId = 1,
- EmployeeId = 5,
- From = new DateTime(2013, 2, 3),
- To = new DateTime(2013, 2, 12)
- });
- modelBuilder.Entity<Reparation>().HasData(new Reparation
- {
- Id = 3,
- TrainId = 4,
- EmployeeId = 19,
- From = new DateTime(2009, 8, 16),
- To = new DateTime(2009, 9, 1)
- });
- modelBuilder.Entity<Reparation>().HasData(new Reparation
- {
- Id = 4,
- TrainId = 4,
- EmployeeId = 5,
- From = new DateTime(2019, 12, 1)
- });
- }
- private static void SeedEmployees(ModelBuilder modelBuilder)
- {
- var location = Assembly.GetExecutingAssembly().Location;
- string dataDirectory = Path.GetDirectoryName(location);
- string fullPath = Path.Combine(dataDirectory, "employees.csv");
- Console.WriteLine($"Seed Employee using file {fullPath}");
- try
- {
- File.ReadAllLines(fullPath)
- .Skip(1)
- .Select(x=>x.Split(';'))
- .ToList()
- .ForEach(x=> {
- modelBuilder.Entity<Employee>().HasData(new Employee
- {
- Id = Int32.Parse(x[0]),
- Firstname = x[1],
- Lastname = x[2],
- Tel = x[3],
- SocialInsuranceNr = x[4],
- IsActive = true
- });
- });
- }
- catch (Exception exc)
- {
- Console.WriteLine($"*****{exc.Message} ---- {exc.InnerException.Message}");
- }
- }
- private static void SeedTrains(ModelBuilder modelBuilder)
- {
- modelBuilder.Entity<Train>().HasData(new Train
- {
- Id = 1,
- TypId = 2,
- Name = "Taigatrommel",
- OwnerId = 1,
- BuildYear = 1995,
- Length = 10
- });
- modelBuilder.Entity<Train>().HasData(new Train
- {
- Id = 2,
- TypId = 2,
- Name = "Voith Gravita",
- OwnerId = 4,
- BuildYear = 2010,
- Length = 9
- });
- modelBuilder.Entity<Train>().HasData(new Train
- {
- Id = 3,
- TypId = 1,
- Name = "Siemens EuroSprinter",
- OwnerId = 2,
- BuildYear = 2001,
- Length = 18
- });
- modelBuilder.Entity<Train>().HasData(new Train
- {
- Id = 4,
- TypId = 1,
- Name = "Bombardier TRAXX",
- OwnerId = 3,
- BuildYear = 2002,
- Length = 12
- });
- modelBuilder.Entity<Train>().HasData(new Train
- {
- Id = 5,
- TypId = 3,
- Name = "Eanos X055",
- OwnerId = 2,
- BuildYear = 1990,
- Length = 13
- });
- modelBuilder.Entity<Train>().HasData(new Train
- {
- Id = 6,
- TypId = 4,
- Name = "DABpza 785.3",
- OwnerId = 2,
- BuildYear = 1990,
- Length = 20
- });
- }
- private static void SeedOwners(ModelBuilder modelBuilder)
- {
- List<string> owners = new List<string>() { "ÖBB", "Deutsche Bahn", "SNCF", "Schweizer Bundesbahnen" };
- for (int i = 0; i < owners.Count; i++)
- {
- modelBuilder.Entity<Owner>().HasData(new Owner
- {
- Id = i + 1,
- Name = owners[i]
- });
- }
- }
- private static void SeedTypes(ModelBuilder modelBuilder)
- {
- List<string> names = new List<string>() { "Elektrolok", "Diesellok", "Güterwaggon", "Personenwaggon" };
- for (int i = 0; i < names.Count; i++)
- {
- modelBuilder.Entity<Type>().HasData(new Type
- {
- Id = i+1,
- Name = names[i]
- });
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement