private static void SeedSingleMarkFromFile(MarksDbContext context, string filePath) { using (TextFieldParser parser = new TextFieldParser(filePath)) { parser.Delimiters = new string[] { "," }; while (true) { string[] parts = parser.ReadFields(); if (parts == null) { break; } var country = parts[0]; var number = parts[1]; DateTime?expDateTime = null; if (!String.IsNullOrWhiteSpace(parts[2])) { expDateTime = DateTime.ParseExact(parts[2], "dd.M.yyyy", CultureInfo.InvariantCulture); } string ownerName = "Open mark"; if (!String.IsNullOrWhiteSpace(parts[3])) { ownerName = parts[3]; } var owner = new Owner { Name = ownerName }; if (!context.Owners.Any(o => o.Name == owner.Name)) { context.Owners.AddOrUpdate(owner); } var notes = ""; if (parts.Length > 4) { notes = parts[4]; } string fileName = Path.GetFileNameWithoutExtension(filePath); var mark = new Mark { Name = fileName, Number = number, Country = context.Countries.FirstOrDefault(x => x.Name == country), ExpirationDate = expDateTime, Owner = context.Owners.FirstOrDefault(x => x.Name == ownerName), Notes = notes }; context.Marks.Add(mark); context.SaveChanges(); } } }
private void SeedCountries(MarksDbContext context) { var countries = new List <Country>() { new Country() { Name = "Albania" }, new Country() { Name = "Algeria" }, new Country() { Name = "Armenia" }, new Country() { Name = "Austria" }, new Country() { Name = "Azerbaijan" }, new Country() { Name = "Bahrain" }, new Country() { Name = "Belarus" }, new Country() { Name = "Belgium" }, new Country() { Name = "Benelux" }, new Country() { Name = "Bosnia and Herzegovina" }, new Country() { Name = "Botswana" }, new Country() { Name = "Bulgaria" }, new Country() { Name = "China" }, new Country() { Name = "Croatia" }, new Country() { Name = "Cyprus" }, new Country() { Name = "Czech Republic" }, new Country() { Name = "Denmark" }, new Country() { Name = "Egypt" }, new Country() { Name = "Estonia" }, new Country() { Name = "Finland" }, new Country() { Name = "France" }, new Country() { Name = "Georgia" }, new Country() { Name = "Germany" }, new Country() { Name = "Ghana" }, new Country() { Name = "Greece" }, new Country() { Name = "Hungary" }, new Country() { Name = "Iceland" }, new Country() { Name = "India" }, new Country() { Name = "Iran" }, new Country() { Name = "Iraq" }, new Country() { Name = "Ireland" }, new Country() { Name = "Israel" }, new Country() { Name = "Italy" }, new Country() { Name = "Japan" }, new Country() { Name = "Kazakhstan" }, new Country() { Name = "Kenya" }, new Country() { Name = "DPR Korea" }, new Country() { Name = "Korea" }, new Country() { Name = "Kosovo" }, new Country() { Name = "Kyrgyzstan" }, new Country() { Name = "Kuwait" }, new Country() { Name = "Latvia" }, new Country() { Name = "Lebanon" }, new Country() { Name = "Lesotho" }, new Country() { Name = "Liberia" }, new Country() { Name = "Liechtenstein" }, new Country() { Name = "Lithuania" }, new Country() { Name = "Luxembourg" }, new Country() { Name = "Macedonia" }, new Country() { Name = "Malawi" }, new Country() { Name = "Malta" }, new Country() { Name = "Moldova" }, new Country() { Name = "Monaco" }, new Country() { Name = "Montenegro" }, new Country() { Name = "Morocco" }, new Country() { Name = "Namibia" }, new Country() { Name = "Netherlands" }, new Country() { Name = "Nigeria" }, new Country() { Name = "Norway" }, new Country() { Name = "OAPI" }, new Country() { Name = "Oman" }, new Country() { Name = "Peru" }, new Country() { Name = "Philippines" }, new Country() { Name = "Poland" }, new Country() { Name = "Portugal" }, new Country() { Name = "Romania" }, new Country() { Name = "Russia" }, new Country() { Name = "Rwanda" }, new Country() { Name = "San Marino" }, new Country() { Name = "Saudi Arabia" }, new Country() { Name = "South Africa" }, new Country() { Name = "Serbia" }, new Country() { Name = "Sierra Leone" }, new Country() { Name = "Singapore" }, new Country() { Name = "Slovakia" }, new Country() { Name = "Slovenia" }, new Country() { Name = "Spain" }, new Country() { Name = "Sudan" }, new Country() { Name = "Swaziland" }, new Country() { Name = "Sweden" }, new Country() { Name = "Switzerland" }, new Country() { Name = "Syria" }, new Country() { Name = "Tajikistan" }, new Country() { Name = "Tanzania" }, new Country() { Name = "Turkey" }, new Country() { Name = "Turkmenistan" }, new Country() { Name = "Tunisia" }, new Country() { Name = "Uganda" }, new Country() { Name = "Ukraine" }, new Country() { Name = "United Arab Emirates" }, new Country() { Name = "United Kingdom (UK)" }, new Country() { Name = "USA" }, new Country() { Name = "Uzbekistan" }, new Country() { Name = "Vietnam" }, new Country() { Name = "Zambia" }, new Country() { Name = "Zimbabwe" } }; foreach (var c in countries) { context.Countries.Add(c); } context.SaveChanges(); }