public Ad AddAd(string title, string version)
 {
     using (_context ?? new AdExpressDBContext())
     {
         var ad = _context.Ads.Add(new Ad {
             Title = title, Version = version
         });
         _context.SaveChanges();
         return(ad);
     }
 }
 public Ad SaveAd(Ad ad)
 {
     using (var context = new AdExpressDBContext())
     {
         context.Set <Ad>().AddOrUpdate(ad);
         context.SaveChanges();
         return(ad);
     }
 }
 public Association SaveAssociation(Association association)
 {
     using (var context = new AdExpressDBContext())
     {
         context.Entry(association).State = EntityState.Modified;
         context.SaveChanges();
         return(association);
     }
 }
 public Newspaper SaveNewspaper(Newspaper newspaper)
 {
     using (var context = new AdExpressDBContext())
     {
         context.Entry(newspaper).State = EntityState.Modified;
         context.SaveChanges();
         return(newspaper);
     }
 }
 public void RemoveAd(Ad ad)
 {
     using (var context = new AdExpressDBContext())
     {
         var p = context.Ads.SingleOrDefault(x => x.ID == ad.ID);
         if (p == null)
         {
             return;
         }
         context.Set <Ad>().Remove(p);
         context.SaveChanges();
     }
 }
 public void RemoveAssociation(Association association)
 {
     using (var context = new AdExpressDBContext())
     {
         var p = context.Associations.SingleOrDefault(x => x.ID == association.ID);
         if (p == null)
         {
             return;
         }
         context.Associations.Remove(p);
         context.SaveChanges();
     }
 }
 public void RemoveNewspaper(Newspaper newspaper)
 {
     using (var context = new AdExpressDBContext())
     {
         var p = context.Newspapers.SingleOrDefault(x => x.ID == newspaper.ID);
         if (p == null)
         {
             return;
         }
         context.Newspapers.Remove(p);
         context.SaveChanges();
     }
 }
 public Association AddAssociation(DateTime startDateTime, DateTime endDateTime, int adID, int newspaperID)
 {
     using (var context = new AdExpressDBContext())
     {
         var ad        = FindAd(adID);
         var newspaper = FindNewspaper(newspaperID);
         if (ad == null || newspaper == null)
         {
             return(null);
         }
         var association =
             context.Associations.Add(new Association
         {
             AdId          = adID,
             StartDateTime = startDateTime,
             EndDateTime   = endDateTime,
             NewspaperId   = newspaperID,
             Ad            = ad,
             Newspaper     = newspaper
         });
         context.SaveChanges();
         return(association);
     }
 }