Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.54 KB | None | 0 0
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8.  
  9. namespace TrainAPIDb
  10. {
  11.     public static class DbSeederExtension
  12.     {
  13.         public static void Seed (this ModelBuilder modelBuilder)
  14.         {
  15.             SeedTypes(modelBuilder);
  16.             SeedOwners(modelBuilder);
  17.             SeedTrains(modelBuilder);
  18.             SeedEmployees(modelBuilder);
  19.             SeedReperations(modelBuilder);
  20.         }
  21.  
  22.         private static void SeedReperations(ModelBuilder modelBuilder)
  23.         {
  24.             modelBuilder.Entity<Reparation>().HasData(new Reparation
  25.             {
  26.                 Id = 1,
  27.                 TrainId = 1,
  28.                 EmployeeId = 37,
  29.                 From = new DateTime(2008,9,14),
  30.                 To = new DateTime(2009,11,2)
  31.             });
  32.             modelBuilder.Entity<Reparation>().HasData(new Reparation
  33.             {
  34.                 Id = 2,
  35.                 TrainId = 1,
  36.                 EmployeeId = 5,
  37.                 From = new DateTime(2013, 2, 3),
  38.                 To = new DateTime(2013, 2, 12)
  39.             });
  40.             modelBuilder.Entity<Reparation>().HasData(new Reparation
  41.             {
  42.                 Id = 3,
  43.                 TrainId = 4,
  44.                 EmployeeId = 19,
  45.                 From = new DateTime(2009, 8, 16),
  46.                 To = new DateTime(2009, 9, 1)
  47.             });
  48.             modelBuilder.Entity<Reparation>().HasData(new Reparation
  49.             {
  50.                 Id = 4,
  51.                 TrainId = 4,
  52.                 EmployeeId = 5,
  53.                 From = new DateTime(2019, 12, 1)
  54.             });
  55.         }
  56.  
  57.         private static void SeedEmployees(ModelBuilder modelBuilder)
  58.         {
  59.             var location = Assembly.GetExecutingAssembly().Location;
  60.             string dataDirectory = Path.GetDirectoryName(location);
  61.             string fullPath = Path.Combine(dataDirectory, "employees.csv");
  62.             Console.WriteLine($"Seed Employee using file {fullPath}");
  63.             try
  64.             {
  65.                 File.ReadAllLines(fullPath)
  66.                     .Skip(1)
  67.                     .Select(x=>x.Split(';'))
  68.                     .ToList()
  69.                     .ForEach(x=> {
  70.                         modelBuilder.Entity<Employee>().HasData(new Employee
  71.                         {
  72.                             Id = Int32.Parse(x[0]),
  73.                             Firstname = x[1],
  74.                             Lastname = x[2],
  75.                             Tel = x[3],
  76.                             SocialInsuranceNr = x[4],
  77.                             IsActive = true
  78.                         });
  79.                     });
  80.             }
  81.             catch (Exception exc)
  82.             {
  83.                 Console.WriteLine($"*****{exc.Message} ---- {exc.InnerException.Message}");
  84.             }
  85.         }
  86.  
  87.         private static void SeedTrains(ModelBuilder modelBuilder)
  88.         {
  89.             modelBuilder.Entity<Train>().HasData(new Train
  90.             {
  91.                 Id = 1,
  92.                 TypId = 2,
  93.                 Name = "Taigatrommel",
  94.                 OwnerId = 1,
  95.                 BuildYear = 1995,
  96.                 Length = 10
  97.             });
  98.             modelBuilder.Entity<Train>().HasData(new Train
  99.             {
  100.                 Id = 2,
  101.                 TypId = 2,
  102.                 Name = "Voith Gravita",
  103.                 OwnerId = 4,
  104.                 BuildYear = 2010,
  105.                 Length = 9
  106.             });
  107.             modelBuilder.Entity<Train>().HasData(new Train
  108.             {
  109.                 Id = 3,
  110.                 TypId = 1,
  111.                 Name = "Siemens EuroSprinter",
  112.                 OwnerId = 2,
  113.                 BuildYear = 2001,
  114.                 Length = 18
  115.             });
  116.             modelBuilder.Entity<Train>().HasData(new Train
  117.             {
  118.                 Id = 4,
  119.                 TypId = 1,
  120.                 Name = "Bombardier TRAXX",
  121.                 OwnerId = 3,
  122.                 BuildYear = 2002,
  123.                 Length = 12
  124.             });
  125.             modelBuilder.Entity<Train>().HasData(new Train
  126.             {
  127.                 Id = 5,
  128.                 TypId = 3,
  129.                 Name = "Eanos X055",
  130.                 OwnerId = 2,
  131.                 BuildYear = 1990,
  132.                 Length = 13
  133.             });
  134.             modelBuilder.Entity<Train>().HasData(new Train
  135.             {
  136.                 Id = 6,
  137.                 TypId = 4,
  138.                 Name = "DABpza 785.3",
  139.                 OwnerId = 2,
  140.                 BuildYear = 1990,
  141.                 Length = 20
  142.             });
  143.         }
  144.  
  145.         private static void SeedOwners(ModelBuilder modelBuilder)
  146.         {
  147.             List<string> owners = new List<string>() { "ÖBB", "Deutsche Bahn", "SNCF", "Schweizer Bundesbahnen" };
  148.             for (int i = 0; i < owners.Count; i++)
  149.             {
  150.                 modelBuilder.Entity<Owner>().HasData(new Owner
  151.                 {
  152.                     Id = i + 1,
  153.                     Name = owners[i]
  154.                 });
  155.             }
  156.         }
  157.  
  158.         private static void SeedTypes(ModelBuilder modelBuilder)
  159.         {
  160.             List<string> names = new List<string>() { "Elektrolok", "Diesellok", "Güterwaggon", "Personenwaggon" };
  161.             for (int i = 0; i < names.Count; i++)
  162.             {
  163.                 modelBuilder.Entity<Type>().HasData(new Type
  164.                 {
  165.                     Id = i+1,
  166.                     Name = names[i]
  167.                 });
  168.             }
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement