//Retrieve the performance from the database with this Id
        public void LoadByName()
        {
            try
            {
                using (MusicEntities dc = new MusicEntities())
                {
                    //Retrieve from the db
                    tblPerformance performance = dc.tblPerformances.FirstOrDefault(p => p.Name == this.Name);

                    //Set this performance's properties
                    this.Id              = performance.Id;
                    this.Description     = performance.Description;
                    this.Location        = performance.Location;
                    this.PerformanceDate = performance.PerformanceDate;
                    this.PDFPath         = performance.pdfPath;
                    this.Name            = performance.Name;

                    LoadPerformancePieces();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int Update()
        {
            try
            {
                using (MusicEntities dc = new MusicEntities())
                {
                    //Retrieve the record from the DB
                    tblPerformance performance = dc.tblPerformances.FirstOrDefault(c => c.Id == this.Id);

                    //Update the properties
                    performance.Name            = this.Name;
                    performance.Description     = this.Description;
                    performance.PerformanceDate = this.PerformanceDate;
                    performance.pdfPath         = this.PDFPath;
                    performance.Location        = this.Location;

                    //Save the changes
                    return(dc.SaveChanges());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void DeleteTest()
        {
            using (MusicEntities dc = new MusicEntities())
            {
                tblPerformance performance = dc.tblPerformances.FirstOrDefault(a => a.Name == "PL Test");

                dc.tblPerformances.Remove(performance);

                dc.SaveChanges();

                tblPerformance retrievedPerformance = dc.tblPerformances.FirstOrDefault(a => a.Name == "PL Test");

                Assert.IsNull(retrievedPerformance);
            }
        }
        public void UpdateTest()
        {
            using (MusicEntities dc = new MusicEntities())
            {
                tblPerformance performance = dc.tblPerformances.FirstOrDefault(a => a.Name == "PL Test");

                performance.Location = "PL Updated Test";;

                dc.SaveChanges();

                tblPerformance retrievedPerformance = dc.tblPerformances.FirstOrDefault(a => a.Name == "PL Test");

                Assert.IsNotNull(retrievedPerformance);
            }
        }
        //Insert the performance into the db, and generate a new Id for it.
        public int Insert()
        {
            try
            {
                using (MusicEntities dc = new MusicEntities())
                {
                    //Create a new Id
                    this.Id = Guid.NewGuid();

                    //Set the properties
                    tblPerformance performance = new tblPerformance
                    {
                        Id              = this.Id,
                        Name            = this.Name,
                        Description     = this.Description,
                        PerformanceDate = this.PerformanceDate,
                        pdfPath         = this.PDFPath,
                        Location        = this.Location
                    };

                    //Add it to the table and save changes
                    dc.tblPerformances.Add(performance);

                    // Send an email to inform that a new performance was added
                    // Shapiro password: $haphir0MT
                    // Test password: MusicTrackerT3st
                    MailMessage mail       = new MailMessage();
                    SmtpClient  smtpClient = new SmtpClient("smtp.gmail.com");

                    mail.From = new MailAddress("*****@*****.**");
                    mail.To.Add("*****@*****.**");
                    mail.Subject = "New Performance Added!";
                    mail.Body    = "A new performance was added! Check it out here: http://shapiro.azurewebsites.net/Performance/Details/" + this.Id;

                    smtpClient.Port        = 587;
                    smtpClient.Credentials = new System.Net.NetworkCredential("*****@*****.**", "$haphir0MT");
                    smtpClient.EnableSsl   = true;

                    smtpClient.Send(mail);

                    return(dc.SaveChanges());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int Delete()
        {
            try
            {
                using (MusicEntities dc = new MusicEntities())
                {
                    //Retrieve it from the DB
                    tblPerformance performance = dc.tblPerformances.FirstOrDefault(p => p.Id == this.Id);

                    //Remove the performance
                    dc.tblPerformances.Remove(performance);

                    //Save the changes
                    return(dc.SaveChanges());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void InsertTest()
        {
            using (MusicEntities dc = new MusicEntities())
            {
                tblPerformance performance = new tblPerformance();
                performance.Id              = Guid.NewGuid();
                performance.Name            = "PL Test";
                performance.PerformanceDate = DateTime.Now;
                performance.Location        = "PL Test";
                performance.pdfPath         = "PL Test";
                performance.Description     = "PL Test";

                dc.tblPerformances.Add(performance);

                dc.SaveChanges();

                tblPerformance retrievedPerformance = dc.tblPerformances.FirstOrDefault(a => a.Name == "PL Test");

                Assert.AreEqual(performance.Id, retrievedPerformance.Id);
            }
        }