public IEnumerable <Student> GetStudents() { var db = new s5554Context(); var students = db.Student.ToList(); return(students); }
public static void InsertExamples() { var db = new s5554Context(); var s = new Student { FirstName = "Kamil", LastName = "Potocki" }; db.Student.Add(s); db.SaveChanges(); }
public Student UpdateStudent(string id, string lastname) { var db = new s5554Context(); // update var st1 = db.Student.Find(id); st1.LastName = lastname; db.SaveChanges(); return(st1); }
public Student RemoveStudent(string id) { var db = new s5554Context(); var s = new Student { IndexNumber = id }; db.Attach(s); db.Remove(s); db.SaveChanges(); return(s); }
public static void RemoveExamples() { var db = new s5554Context(); // usuwanie studenta pobierajac go najpierw /* var st = db.Student.OrderByDescending(s => s.IndexNumber).First(); * db.Student.Remove(st);*/ var s = new Student { IndexNumber = "111" }; db.Attach(s); // dolaczanie obiektu db.Remove(s); // ustawienie flagi na Deleted i usiniecie //db.Entry(s).State = EntityState.Deleted; to samo co dwie komendy powyzej db.SaveChanges(); }
public static void QueryExamples() { var db = new s5554Context(); // SELECT * FROM Student //var students = db.Student.ToList(); // wszyscy studenci ktorych imie zaczyna sie na /* var student = db.Student * .Where(s => s.FirstName.StartsWith("K")) * .OrderBy(s => s.LastName) * .ThenBy(s => s.FirstName) * .ToList();*/ // Lazy Loading /* var student = db.Student * .Where(s => s.FirstName.StartsWith("K")) * .OrderBy(s => s.LastName) * .ThenBy(s => s.FirstName);*/ /* var students = db.Student.ToList(); * * int g = 0; * // problem N+1 * foreach(var s in students) // tu jest SELECT * FROM Student * { * * }*/ //var student = db.Student.OrderBy(s => s.LastName); //var student2 = student.Where(s => s.LastName == "Potocki"); /*var students = db.Student * .GroupBy(s => s.FirstName) * .Select(s => new * { * Imie = s.Key, * LiczbaStudentow = s.Count() * }).ToList();*/ }
public static void UpdateExamples() { var db = new s5554Context(); // update /*var st1 = db.Student.First(); // pierwszy rekord z bazy * st1.LastName = "Zmiana";*/ //db.SaveChanges(); // wysylanie zmian z kolekcji sa wysylane w jednej transakcji do bazy //db.SaveChangesAsync(); // update studenta ktorego jeszcze nie ma, zostanie on stworzony var st1 = new Student { IndexNumber = "8", LastName = "Drewniany" }; db.Attach(st1); //db.Entry(st1).Property("LastName").IsModified = true; db.SaveChanges(); }