/// <summary> /// Create a new Department object. /// </summary> /// <param name="departmentID">Initial value of the DepartmentID property.</param> public static Department CreateDepartment(global::System.Int32 departmentID) { Department department = new Department(); department.DepartmentID = departmentID; return department; }
/// <summary> /// 利用Foreign Key Association来插入一条新的Course和Department关联实体 /// </summary> private static void InsertNewRelatedEntities() { using (FKAssociationEntities context = new FKAssociationEntities()) { // 创建一个新的Department实体。 Department department = new Department() { DepartmentID = 5, Name = "Computer Science", Budget = 400000, StartDate = DateTime.Now }; // 创建一个新的Course实体。 Course course = new Course() { CourseID = 5001, Title = "Operation System", Credits = 4, StatusID = true, // 设置它的外键属性。 DepartmentID = department.DepartmentID }; try { // 将Department和Course实体添加到ObjectContext。 context.AddToDepartments(department); context.AddToCourses(course); // 注意: Department和Course实体之间的导航属性在SaveChanges方法未被调用前不会映射到彼此。 context.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
/// <summary> /// Deprecated Method for adding a new object to the Departments EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToDepartments(Department department) { base.AddObject("Departments", department); }