/// <summary> /// Independent Association下插入一条新的Course并将其设置成附属于一个已存在的Department。 /// </summary> private static void InsertByExistingEntities() { using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { // 创建一个新的Course实体。 Course course = new Course() { CourseID = 6002, Title = ".NET Framework", Credits = 4, StatusID = true, // 设置导航属性为已存在的Department实体 Department = context.Departments.Single( d => d.DepartmentID == 6) }; try { // 注意: 不需要将Course实体添加进ObjectContext,因为它 // 被自动关联到了一个已存在的Department实体。 context.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
/// <summary> /// 更新一个已存在的Course实体(只有常规属性,不包括关系) /// </summary> /// <param name="updatedCourse">一个已存在的Course实体和更新的数据</param> private static void UpdateExistingEntities(Course updatedCourse) { using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { try { // 查询数据库来获取要更新的实体 var originalCourse = context.Courses.SingleOrDefault( c => c.CourseID == updatedCourse.CourseID); ///////////////////////////////////////////////////////// // 使用ApplyCurrentValues API来更新实体 // // 在EFv1时代,EFv1中的ApplyPropertyChanges方法能够使用其他实体的值来更新一个实体。 // 在EF4时代,此方法更名为ApplyCurrentValues,此方法打算用来更新一个实体的当前值, // 假定是刚从数据库提取的实体。这将会允许SaveChanges方法创建适当的命令来将那些新数据 // 更新到实体。 // 适用于最新的Course实体包括外键属性的原始值。 // 注意: 在这里,导航属性就算被更改了也不会被更新。 if (originalCourse != null) { context.Courses.ApplyCurrentValues(updatedCourse); } // 保存更改。 context.SaveChanges(); /* ///////////////////////////////////////////////////////// // 使用ApplyOriginalValues API来更新实体 // // ApplyOriginalValues假设所被追踪的实体已经拥有需要被更新到数据库的数据,而非实体的原始数据。 // 这是为n层应用程序设计的。通过在拥有新数据的实体上调用Attach方法,实体将自动变为Unchanged状态, // 并且拥有相应匹配的当前值和过去值。ApplyOriginalValues帮助我们通过分离实体来使用实体的原始值。 // 为了展示ApplyOriginalValues,我们首先分离原始的Course实体。 context.Detach(originalCourse); // 附加一个新的更新的Course实体 context.Courses.Attach(updatedCourse); // 适用于最新的Course实体包括外键属性的原始值。 // 注意: 在这里,导航属性就算被更改了也不会被更新。 context.Courses.ApplyOriginalValues(originalCourse); // 保存更改。 context.SaveChanges(); */ } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
/// <summary> /// Create a new Course object. /// </summary> /// <param name="courseID">Initial value of the CourseID property.</param> public static Course CreateCourse(global::System.Int32 courseID) { Course course = new Course(); course.CourseID = courseID; return course; }
/// <summary> /// Independent Association下插入一条新的Course和Department关联实体 /// </summary> private static void InsertNewRelatedEntities() { using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { // 创建一个新的Department实体。 Department department = new Department() { DepartmentID = 6, Name = "Software Engineering", Budget = 300000, StartDate = DateTime.Now }; // 创建一个新的Course实体。 Course course = new Course() { CourseID = 6001, Title = "Object Oriented", Credits = 4, StatusID = true, // 设置导航属性。 Department = department }; try { // 注意: 只需要添加一个实体,因为关系和关联实体将自动被添加。 context.AddToCourses(course); context.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
/// <summary> /// Deprecated Method for adding a new object to the Courses EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToCourses(Course course) { base.AddObject("Courses", course); }