kema

BookShopSystem-Configuration

Mar 19th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. namespace BookShopSystem.Data.Migrations
  2. {
  3.     using Models;
  4.     using System;
  5.     using System.Data.Entity.Migrations;
  6.     using System.Globalization;
  7.     using System.IO;
  8.     using System.Linq;
  9.  
  10.     public sealed class Configuration : DbMigrationsConfiguration<BookShopDbContext>
  11.     {
  12.         public Configuration()
  13.         {
  14.             AutomaticMigrationsEnabled = true;
  15.             ContextKey = "BookShopSystem.Data.BookShopDbContext";
  16.             AutomaticMigrationDataLossAllowed = true;
  17.         }
  18.  
  19.         protected override void Seed(BookShopDbContext context)
  20.         {
  21.             SeedAuthors(context);
  22.             SeedBooks(context);
  23.             SeedCategories(context);
  24.         }
  25.         private static void SeedCategories(BookShopDbContext context)
  26.         {
  27.             int booksCount = context.Books.Local.Count;
  28.             string[] categories = File.ReadAllLines("C:/Users/Kemi/Documents/Visual Studio 2015/Projects/ExerciseRelationsAdvanced/BookShopSystem/BookShopSystem.Data/Import/categories.csv");
  29.  
  30.             for (int i = 1; i < categories.Length; i++)
  31.             {
  32.                 string[] data = categories[i]
  33.                   .Split(',')
  34.                   .Select(arg => arg.Replace("\"", string.Empty))
  35.                   .ToArray();
  36.  
  37.                 string categoryName = categories[0];
  38.                 Category category = new Category() { Name = categoryName };
  39.  
  40.                 int bookIndex = (i * 30) % booksCount;
  41.                 for (int j = 0; j < bookIndex; j++)
  42.                 {
  43.                     Book book = context.Books.Local[j];
  44.                     category.Books.Add(book);
  45.                 }
  46.  
  47.                 context.Categories.AddOrUpdate(c => c.Name, category);
  48.             }
  49.         }
  50.  
  51.         private static void SeedBooks(BookShopDbContext context)
  52.         {
  53.             int authorsCount = context.Authors.Local.Count;
  54.             string[] books = File.ReadAllLines("C:/Users/Kemi/Documents/Visual Studio 2015/Projects/ExerciseRelationsAdvanced/BookShopSystem/BookShopSystem.Data/Import/books.csv");
  55.  
  56.             for (int i = 1; i < books.Length; i++)
  57.             {
  58.                 string[] data = books[i]
  59.                     .Split(',')
  60.                     .Select(arg => arg.Replace("\"", string.Empty))
  61.                     .ToArray();
  62.  
  63.                 int authorIndex = i % authorsCount;
  64.                 Author author = context.Authors.Local[authorIndex];
  65.                 EditionType edition = (EditionType)int.Parse(data[0]);
  66.                 DateTime releaseDate = DateTime.ParseExact(data[1], "d/M/yyyy", CultureInfo.InvariantCulture);
  67.                 int copies = int.Parse(data[2]);
  68.                 decimal price = decimal.Parse(data[3]);
  69.                 AgeRestriction ageRestriction = (AgeRestriction)int.Parse(data[4]);
  70.                 string title = data[5];
  71.                 Book book = new Book
  72.                 {
  73.                     Author = author,
  74.                     AuthorId = author.Id,
  75.                     EditionType = edition,
  76.                     ReleaseDate = releaseDate,
  77.                     Copies = copies,
  78.                     Price = price,
  79.                     AgeRestriction = ageRestriction,
  80.                     Title = title
  81.                 };
  82.                 context.Books.AddOrUpdate(b => new { b.Title, b.AuthorId }, book);
  83.             }
  84.         }
  85.  
  86.         private static void SeedAuthors(BookShopDbContext context)
  87.         {
  88.             string[] authors = File.ReadAllLines("C:/Users/Kemi/Documents/Visual Studio 2015/Projects/ExerciseRelationsAdvanced/BookShopSystem/BookShopSystem.Data/Import/authors.csv");
  89.  
  90.             for (int i = 1; i < authors.Length; i++)
  91.             {
  92.                 string[] data = authors[i].Split(',');
  93.                 string firstName = data[0].Replace("\"", string.Empty);
  94.                 string lastName = data[1].Replace("\"", string.Empty);
  95.                 Author author = new Author()
  96.                 {
  97.                     FirstName = firstName,
  98.                     LastName = lastName
  99.                 };
  100.  
  101.                 // When adding we may want to check if author with same first name and last name is existing.
  102.                 context.Authors.AddOrUpdate(a => new { a.FirstName, a.LastName }, author);
  103.             }
  104.         }
  105.  
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment