public static void addUsers(List <User> users) { using (var db = new BookClubDB()) { foreach (var u in users) { db.Users.Add(u); try { db.SaveChanges(); Console.WriteLine("User " + u.FirstName + " " + u.LastName + " added"); } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) { Console.WriteLine("Duplicate found"); db.Users.Remove(u); db.SaveChanges(); } } } }
public static void addReviews(List <Review> reviews) { using (var db = new BookClubDB()) { foreach (var r in reviews) { db.Reviews.Add(r); try { db.SaveChanges(); Console.WriteLine("Review of " + r.UserName + " added"); } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) { Console.WriteLine("Duplicate found"); db.Reviews.Remove(r); db.SaveChanges(); } } } }
public static void addAuthors(List <Author> authors) { Console.WriteLine("adding authors to the database"); using (var db = new BookClubDB()) { foreach (var a in authors) { db.Authors.Add(a); try { db.SaveChanges(); Console.WriteLine("author " + a.FirstName + " " + a.LastName + " added"); } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) { Console.WriteLine("Duplicate found"); db.Authors.Remove(a); db.SaveChanges(); } } } }