public ActionResult Create([Bind(Include = "Id,Name")] Student student) { if (ModelState.IsValid) { db.Students.Add(student); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(student)); }
public ActionResult Create([Bind(Include = "StaffId,Specialty")] Teacher teacher) { if (ModelState.IsValid) { db.Teachers.Add(teacher); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.StaffId = new SelectList(db.Staff, "Id", "Name", teacher.StaffId); return(View(teacher)); }
public ActionResult Create([Bind(Include = "Id,Name,Salary")] Staff staff) { if (ModelState.IsValid) { db.Staff.Add(staff); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Id = new SelectList(db.Teachers, "StaffId", "Specialty", staff.Id); return(View(staff)); }
public ActionResult Create([Bind(Include = "Id,Name,TeacherId")] Course course) { if (ModelState.IsValid) { db.Courses.Add(course); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.TeacherId = new SelectList(db.Teachers, "StaffId", "Specialty", course.TeacherId); return(View(course)); }
static void Main(string[] args) { //display name using (var db = new EfExampleEntities()) { var students = db.Students.Select(x => x.Name); foreach (var student in students) { Console.WriteLine(student); } } Console.WriteLine(); //display course using (var db = new EfExampleEntities()) { var students = db.Students; foreach (var student in students) { Console.WriteLine(student.Name + ":"); foreach (var course in student.Courses) { Console.WriteLine(course.Name); } } } //display teacher for student using (var db = new EfExampleEntities()) { var students = db.Students; foreach (var student in students) { Console.WriteLine(student.Name + ":"); foreach (var course in student.Courses) { Console.WriteLine(course.Teacher.Staff.Name); } } } //display staff thats not teacher using (var db = new EfExampleEntities()) { var staff = db.Staff.Where(s => s.Teacher == null); foreach (var staffmember in staff) { Console.WriteLine(staffmember.Name); } } //names of students in hist101 using (var db = new EfExampleEntities()) { foreach (var student in db.Students.Where(s => s.Courses.Any(c => c.Name == "math101"))) { Console.WriteLine(student.Name); } } //mikes (8) ass got fired and barbra gets his classes barbs id = 7 using (var db = new EfExampleEntities()) { var barbara = db.Teachers.Find(7); var courses = db.Teachers.Find(8).Courses.ToList(); foreach (var course in courses) { course.Teacher = barbara; db.Entry(course).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); } Console.ReadKey(true); }
static void Main(string[] args) { void Print <T>(IEnumerable <T> things) { foreach (T thing in things) { Console.Write(thing + " "); } Console.WriteLine(); } EfExampleEntities db = new EfExampleEntities(); using (db) { //Print the names of all the students. Print(db.Students.Select(x => x.Name)); //Print the names of all the courses. Print(db.Courses.Select(x => x.Name)); //Print the number of courses that have no students enrolled. Console.WriteLine(db.Courses.Count(x => x.Students.Count() == 0)); //Print the names of all the teachers. Print(db.Staff.Where(x => x.Teacher != null).Select(x => x.Name)); //Remove "Mike Teacher" from the database. var mike = db.Teachers.First(x => x.Staff.Name == "Mike Teacher"); mike.Courses.Select((x) => x.Teacher = null); db.Entry(mike.Staff).State = System.Data.Entity.EntityState.Deleted; db.Entry(mike).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); //Print the names of all the teachers again. Print(db.Staff.Where(x => x.Teacher != null).Select(x => x.Name)); //Give every teacher a 1.5 % raise. var res = db.Staff.Where(x => x.Teacher != null); var cours = db.Courses; foreach (Staff staff in res) { staff.Salary = staff.Salary + (int)(0.015 * staff.Salary); db.Entry(staff).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); Print(db.Staff.Where(x => x.Teacher != null).Select(x => x.Salary)); //Give every staff member who is NOT a teacher a 30 % raise. var res2 = db.Staff.Where(x => x.Teacher == null); foreach (Staff staff in res2) { staff.Salary = staff.Salary + (int)(0.3 * staff.Salary); db.Entry(staff).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); Print(db.Staff.Where(x => x.Teacher == null).Select(x => x.Salary)); //Take John Student out of the HIST101 class. var john = db.Students.Single(x => x.Name == "John Student"); var course = db.Courses.Single(x => x.Name == "Hist101"); course.Students.Remove(john); john.Courses.Remove(course); db.Entry(john).State = System.Data.Entity.EntityState.Modified; db.Entry(course).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); Print(john.Courses.Select(x => x.Name)); //EXTRA(OPTIONAL) : Make a new console application and a new database.Using Entity Framework Code First, recreate the same tables and foreign key constraints that EfExample has. db.Dispose(); } Console.ReadKey(true); }