private static void UpdateDepartment(ContosoUniversityEntities db) { var dept = db.Department.Find(InsertedId); dept.Name = "John"; db.SaveChanges(); }
private static void Main(string[] args) { using (var db = new ContosoUniversityEntities()) { Query(db); InsertDepartment(db); UpdateDepartment(db); RemoveDepartment(db); } }
private static void Query(ContosoUniversityEntities db) { var data = from p in db.Course select p; foreach (var item in data) { Console.WriteLine(item.CourseID); Console.WriteLine(item.Title); Console.WriteLine(); } }
private static void GetCourse_Git(ContosoUniversityEntities db) { //var data = db.Course.Where(p => p.Title.Contains("Git")).ToList(); var data = (from p in db.Course where p.Title.Contains("Git") select p).ToList(); foreach (var item in data) { Console.WriteLine(item.Title + "\t" + item.Department.Name); } }
public static void InsertDepartment(ContosoUniversityEntities db) { var dept = new Department() { Name = "Will", Budget = 100, StartDate = DateTime.Now }; db.Department.Add(dept); db.SaveChanges(); InsertedId = dept.DepartmentID; }
static void Main(string[] args) { using (var db = new ContosoUniversityEntities()) { db.Database.Log = (sql) => { Console.WriteLine(sql); }; //GetCourse_Git(db); var data = (from p in db.Department.Include("Course") select p); // foreach (var dept in data) { Console.WriteLine(dept.Name); foreach (var course in dept.Course) { Console.WriteLine("\t" + course.Title); } } } Console.ReadLine(); }
private static void RemoveDepartment(ContosoUniversityEntities db) { db.Department.Remove(db.Department.Find(InsertedId)); db.SaveChanges(); }