public static void GuardarStudentDB() { //Ejemplo de guardar en DB Contexto context = new Contexto(); try { var auxStudent = new Studen() { StudentId = 0, FirstName = "Emminton Manuel", LastName = "Ueña Santana" }; context.Students.Add(auxStudent); bool save = context.SaveChanges() > 0; if (save) { Console.WriteLine("The Student was sucessfully saved!!"); } else { Console.WriteLine("We cant save the student.."); } } catch (Exception) { throw; } finally { context.Dispose(); } }
public async Task <int> create(Studen studen) { studen.CreatedDate = DateTime.Now; studen.CreatedBy = "System"; _dbContext.Studens.Add(studen); await _dbContext.SaveChangesAsync(); return(1); }
public async Task <int> update(int id, Studen studen) { if (id != studen.ID) { return(0); } var modal = _dbContext.Studens.Find(studen.ID); modal.Name = studen.Name; modal.Address = studen.Address; modal.Email = studen.Email; modal.UpdatedDate = DateTime.Now; modal.UpdatedBy = "System"; _dbContext.Entry(modal).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); return(1); }
public static void UpdatingOnDisconnectedScenario() { Contexto context = new Contexto(); try { var modifiedStudent1 = new Studen() { StudentId = 1, FirstName = "jerson", LastName = "gutierrez" }; var modifiedStudent2 = new Studen() { StudentId = 2, FirstName = "Steve", LastName = "mendez" }; List <Studen> modifiedStudents = new List <Studen>() { modifiedStudent1, modifiedStudent2, }; context.UpdateRange(modifiedStudents); bool modified = context.SaveChanges() > 0; if (modified) { Console.WriteLine("Modified"); } } catch (Exception) { throw; } finally { context.Dispose(); } }