示例#1
0
 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();
             }
         }
     }
 }
示例#2
0
 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();
             }
         }
     }
 }
示例#3
0
 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();
             }
         }
     }
 }