public static int SaveVolunteer(Models.EventVolunteer eventVolunteer)
        {
            using (var db = new Context.SqlContext())
            {
                db.Entry(eventVolunteer).State = eventVolunteer.EventVolunteerId.Equals(0) ? EntityState.Added : EntityState.Modified;
                if (db.Entry(eventVolunteer).State == EntityState.Added)
                {
                    //Load School event
                    if (eventVolunteer.SchoolEvent == null)
                    {
                        db.Entry(eventVolunteer).Reference(p => p.SchoolEvent).Load();
                    }
                    //Prevent new volunteer logic:
                    if (eventVolunteer.SchoolEvent.End <= DateTime.Now)
                    {
                        return(-1);                                               //can't register for event that has already happened
                    }
                    if (!eventVolunteer.SchoolEvent.IsVolunteerOpportunity)
                    {
                        return(-1);                                                   //can't register for event that isn't a volunteer opportunity
                    }
                    if (eventVolunteer.SchoolEvent.MaxVolunteers > 0 && eventVolunteer.SchoolEvent.RegisteredVolunteers >= eventVolunteer.SchoolEvent.MaxVolunteers)
                    {
                        return(-1);                                                                                                                                            //can't register for event that has reached the max volunteers allowed
                    }
                    eventVolunteer.RegisteredDate = DateTime.Now;
                    eventVolunteer.SchoolEvent.RegisteredVolunteers++;
                    var ret = db.SaveChanges();
                    //Send email notification to primary contact and new volunteer
                    if ((!string.IsNullOrEmpty(eventVolunteer.Email)) || string.IsNullOrEmpty(eventVolunteer.SchoolEvent.PrimaryContactsEmail))
                    {
                        List <string> to = new List <string>();
                        to.Add(eventVolunteer.Email);
                        List <string> cc = new List <string>();
                        if (!string.IsNullOrEmpty(eventVolunteer.SchoolEvent.PrimaryContactsEmail))
                        {
                            cc.Add(eventVolunteer.SchoolEvent.PrimaryContactsEmail);
                        }
                        string subject = "You volunteered for \"" + eventVolunteer.SchoolEvent.Title + "\" (starts on " + eventVolunteer.SchoolEvent.Start + ")";
                        string body    = "Thank you, " + eventVolunteer.FirstName + ", for volunteering for the \"" + eventVolunteer.SchoolEvent.Title + "\" event. <br>" +
                                         "<b>Start</b>: " + string.Format("{0:f}", eventVolunteer.SchoolEvent.Start) + "<br>" +
                                         "<b>End</b>: " + string.Format("{0:f}", eventVolunteer.SchoolEvent.End) + "<br>" +
                                         (!string.IsNullOrEmpty(eventVolunteer.SchoolEvent.Location) ? ("<b>Location</b>: " + eventVolunteer.SchoolEvent.Location + "<br>") : "") +
                                         (!string.IsNullOrEmpty(eventVolunteer.SchoolEvent.Description) ? ("<b>Description</b>: " + eventVolunteer.SchoolEvent.Description + "<br>") : "");
                        bool emailsent = SendEventEmail(to, cc, null, subject, body);
                    }

                    return(ret);
                }
                else
                {
                    return(db.SaveChanges());
                }
            }
        }
        public static int UnVolunteer(Models.EventVolunteer eventVolunteer)
        {
            using (var db = new Context.SqlContext())
            {
                try
                {
                    db.Configuration.LazyLoadingEnabled = false;
                    if (eventVolunteer.EventVolunteerId.Equals(0))
                    {
                        return(-1);                                           //can't delete a record that hasn't been created in the db yet
                    }
                    if (eventVolunteer.SchoolEvent == null)
                    {
                        db.Entry(eventVolunteer).Reference("SchoolEvent").Load();
                    }
                    eventVolunteer.SchoolEvent.RegisteredVolunteers = eventVolunteer.SchoolEvent.RegisteredVolunteers <= 0 ? 0 : eventVolunteer.SchoolEvent.RegisteredVolunteers - 1;
                    db.Entry(eventVolunteer.SchoolEvent).State      = EntityState.Modified;
                    db.Entry(eventVolunteer).State = EntityState.Deleted;
                    var ret = db.SaveChanges();
                    //Send email notification to primary contact the volunteer
                    if ((!string.IsNullOrEmpty(eventVolunteer.Email)) || string.IsNullOrEmpty(eventVolunteer.SchoolEvent.PrimaryContactsEmail))
                    {
                        List <string> to = new List <string>();
                        to.Add(eventVolunteer.Email);
                        List <string> cc = new List <string>();
                        if ((eventVolunteer.SchoolEvent != null) && (!string.IsNullOrEmpty(eventVolunteer.SchoolEvent.PrimaryContactsEmail)))
                        {
                            cc.Add(eventVolunteer.SchoolEvent.PrimaryContactsEmail);
                        }
                        string subject = "Canceled: \"" + eventVolunteer.SchoolEvent.Title + "\", on " + eventVolunteer.SchoolEvent.Start;
                        string body    = "Thank you, " + eventVolunteer.FirstName + ", for notifying us that you need to cancel for the \"" + eventVolunteer.SchoolEvent.Title +
                                         "\" event that starts " + string.Format("{0:f}", eventVolunteer.SchoolEvent.Start) + ".";
                        bool emailsent = SendEventEmail(to, cc, null, subject, body);
                    }

                    return(ret);
                }
                catch (Exception ex)
                {
                    var e = ex.Message;
                    return(-1);
                }
            }
        }