/// <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); } }
/// <summary> /// If StudentId is 0 they will be added /// otherwise it will update based on the StudentId /// </summary> /// <param name="s">The student to be added or updated</param> 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 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); } }