示例#1
0
        /// <summary>
        /// Deletes student from database, by their studentID
        /// Disconnected Scenario, save trip to DB
        /// See Also: https://www.tektutorialshub.com/entity-framework/ef-deleting-records/
        /// </summary>
        /// <param name="s"></param>
        public static void Delete(Student s)
        {
            using (StudentContext context = new StudentContext()) //using statement vs using directive at the top of the page
            {
#if DEBUG
                context.Database.Log = Console.WriteLine; // Log query generated to output window
#endif
                //context.Students.Add(s); // This looks odd, as we are not trying to reinsert s into the database
                context.Students.Attach(s);
                context.Entry(s).State = EntityState.Deleted; // removed System.Data.Entity & added using
                context.SaveChanges();
            }

            //// This is what a using statment generates:
            //// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement
            //// manual dispose, the using statement above does this for us
            //var context2 = new StudentContext();
            //try
            //{
            //    //DB Code goes here
            //}
            //finally
            //{
            //    context2.Dispose();
            //}
        }
        /// <summary>
        /// deletes a student from the database by their studentID
        /// </summary>
        /// <param name="s"></param>
        public static void Delete(Student s)
        {
            using (var context = new StudentContext())
            {
#if DEBUG
                //log query generate to output window
                context.Database.Log = Console.WriteLine;
#endif

                context.Students.Attach(s);
                context.Entry(s).State = EntityState.Deleted;
                context.SaveChanges();
            }

            //same as above

            /*var context2 = new StudentContext();
             * try
             * {
             *  //DB code goes here
             * }
             * finally
             * {
             *  context2.Dispose();
             * }*/
        }
 /// <summary>
 /// if StudentID is 0, will be added
 /// else will be updated based on StudentID
 /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 public static Student AddOrUpdate(Student s)
 {
     using (var context = new StudentContext())
     {
         context.Entry(s).State = (s.StudentId == 0) ? EntityState.Added : EntityState.Modified;
         context.SaveChanges();
         return(s);
     }
 }
 /// <summary>
 /// updates all student data except for the primary key
 /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 public static Student Update(Student s)
 {
     using (var context = new StudentContext())
     {
         context.Students.Attach(s);
         context.Entry(s).State = EntityState.Modified;
         context.SaveChanges();
         return(s);
     }
 }
示例#5
0
        public static Student Add(Student stu)
        {
            // Check Panopto video 1/27 to see how to wrap using around this.
            StudentContext context = new StudentContext();

            context.Students.Add(stu); // Preparing insert query
            context.SaveChanges();     // Execute insert query against DB

            return(stu);               // Return student with StudentId set
        }
示例#6
0
 /// <summary>
 /// If StudentId = 0 they will be added
 /// else, it will update based upon StudentId
 /// </summary>
 /// <exception cref="System.Data.Entity.Infrastructure.DbUpdateConcurrencyException"
 /// <param name="s"></param>
 /// <returns></returns>
 public static Student AddOrUpdate(Student s)
 {
     using (StudentContext context = new StudentContext())
     {
         //ternary operator, https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator
         context.Entry(s).State = (s.StudentId == 0) ? EntityState.Added : EntityState.Modified;
         context.SaveChanges();
         return(s);
     }
 }
        public static Student Add(Student stu)
        {
            using (StudentContext context = new StudentContext())
            {
                //preparing insert query
                context.Students.Add(stu);
                //execute insert query against DB
                context.SaveChanges();
            }

            //return the student with the studentID set
            return(stu);
        }
示例#8
0
        /// <summary>
        /// Updates all student data except for PrimaryKey
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Student Update(Student s)
        {
            using (StudentContext context = new StudentContext()) //using statement vs using directive at the top of the page
            {
#if DEBUG
                context.Database.Log = Console.WriteLine; // Log query generated to output window
#endif
                context.Students.Attach(s);
                context.Entry(s).State = EntityState.Modified;
                context.SaveChanges();
                return(s);
            }
        }
        /// <summary>
        /// Updates all student data (except for PK)
        /// </summary>
        /// <param name="s">The student to be updated</param>
        public static Student Update(Student s)
        {
            using (var context = new StudentContext())
            {
#if DEBUG
                context.Database.Log = Console.WriteLine;
#endif
                context.Students.Attach(s);
                context.Entry(s).State = EntityState.Modified;
                context.SaveChanges();

                return(s);
            }
        }
        public static Student Add(Student stu)
        {
            using (var context = new StudentContext())
            {
#if DEBUG
                // Log query generated to output window
                context.Database.Log = Console.WriteLine;
#endif
                // Generating/preparing insert query
                context.Students.Add(stu);
                // Executing insert query against DB
                context.SaveChanges();

                // Return the student with the StudentId set
                return(stu);
            }
        }