public void Save()
        {
            var student = new Person();

            try
            {
                using (var context = new StumasysContext())
                {
                    if (this.PersonID == 0)
                    {
                        context.Entry(this).State = EntityState.Added;
                    }
                    else
                    {
                        /*
                         * context.Entry(this).State = EntityState.Modified;
                         * context.SaveChanges();
                         * Person p = context.Person.Include(a => a.Courses).ToList().Find(ca => ca.PersonID == this.PersonID);
                         * p.Courses.Clear();
                         * foreach(var item in this.Courses)
                         * {
                         *  Course cc = context.Course.Find(item);
                         *  p.Courses.Add(cc);
                         * }
                         *
                         * List<Course> bkCourses = new List<Course>();
                         * Person p = context.Person.Include(a => a.Courses).ToList().Find(ca => ca.PersonID == this.PersonID);
                         * bkCourses = p.Courses.ToList();
                         * foreach (Course item in bkCourses)
                         * {
                         *  context.Entry(item).State = EntityState.Detached;
                         * }
                         * p.Courses.Clear();
                         * context.Entry(p).State = EntityState.Modified;
                         */
                        context.Database.ExecuteSqlCommand(
                            "DELETE FROM StudentGrade WHERE PersonID = @id",
                            new SqlParameter("id", this.PersonID)
                            );

                        var bkCourses = this.Courses;
                        this.Courses = null;
                        context.Entry(this).State = EntityState.Modified;
                        this.Courses = bkCourses;
                    }

                    foreach (var item in this.Courses)
                    {
                        context.Entry(item).State = EntityState.Unchanged;
                    }

                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 public void Attach(Attachment file)
 {
     try
     {
         using (var context = new StumasysContext())
         {
             context.Entry(this).State = EntityState.Unchanged;
             context.Entry(file).State = EntityState.Added;
             context.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
 public void Delete(int personId)
 {
     try
     {
         using (var context = new StumasysContext())
         {
             context.Entry(new Person {
                 PersonID = personId
             }).State = EntityState.Deleted;
             context.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }