Exemplo n.º 1
0
 public WSRContext()
 {
     //if (!(Database.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists())
     Database.EnsureDeleted();
     Database.EnsureCreated();
     if (!Participants.Any())
     {
         string DPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\"));
         DPath += "\\CSV\\";
         Database.OpenConnection();
         using (var reader = new StreamReader(DPath + "Participants.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 Participants.AddRange(csv.GetRecords <Participant>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Participants ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Participants OFF;");
         using (var reader = new StreamReader(DPath + "Experts.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 Experts.AddRange(csv.GetRecords <Expert>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Experts ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Experts OFF;");
         using (var reader = new StreamReader(DPath + "Admins.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 Administrators.AddRange(csv.GetRecords <Administrator>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Administrators ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Administrators OFF;");
         using (var reader = new StreamReader(DPath + "Coordinators.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 Coordinators.AddRange(csv.GetRecords <Coordinator>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Coordinators ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Coordinators OFF;");
         List <Competention> temp = new List <Competention>();
         using (var reader = new StreamReader(DPath + "Competentions.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 temp.AddRange(csv.GetRecords <Competention>());
             }
         using (var reader = new StreamReader(DPath + "CompetentionDesc.txt", Encoding.Default))
         {
             Competention CurrC;
             for (int Counter = 1; Counter <= temp.Count; Counter++)//relies on competencies being consequentioal from 1
             {
                 CurrC             = temp.FirstOrDefault(c => c.Id == Counter);
                 CurrC.Description = reader.ReadLine();
             }
         }
         Competentions.AddRange(temp);
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Competentions ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Competentions OFF;");
         using (var reader = new StreamReader(DPath + "Championships.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 Championships.AddRange(csv.GetRecords <Championship>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Championships ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Championships OFF;");
         using (var reader = new StreamReader(DPath + "Infrastructures.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 Infrastructures.AddRange(csv.GetRecords <Infrastructure>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Infrastructures ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Infrastructures OFF;");
         using (var reader = new StreamReader(DPath + "SMPs.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 SMPs.AddRange(csv.GetRecords <SMP>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.SMPs ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.SMPs OFF;");
         using (var reader = new StreamReader(DPath + "Results.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 Results.AddRange(csv.GetRecords <Result>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Results ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Results OFF;");
         using (var reader = new StreamReader(DPath + "Sponsors.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 Sponsors.AddRange(csv.GetRecords <Sponsor>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Sponsors ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Sponsors OFF;");
         using (var reader = new StreamReader(DPath + "Volunteers.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                 Volunteers.AddRange(csv.GetRecords <Volunteer>());
             }
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Volunteers ON;");
         SaveChanges();
         Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Volunteers OFF;");
         Database.CloseConnection();
     }
 }