/// <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> /// Update an existing Course entity (only its regular properties, /// not for the relationship) /// </summary> /// <param name="updatedCourse">An existing Course entity with /// updated data</param> private static void UpdateExistingEntities(Course updatedCourse) { using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { try { // Attach a new Course entity by the primary key of the // updated Course entity. context.Courses.Attach(new Course() { CourseID = updatedCourse.CourseID }); // Apply the updated regular properties to the attached // Course entity. // Note: the navigation property is not updated here // even if it is modified. context.Courses.ApplyCurrentValues(updatedCourse); context.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
/// <summary> /// Insert a new Course and set it belong to an existing Department /// by Independent Association /// </summary> private static void InsertByExistingEntities() { using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { // Create a new Course entity. Course course = new Course() { CourseID = 6002, Title = ".NET Framework", Credits = 4, StatusID = true, // Set the navigation property to an existing Department // entity. Department = context.Departments.Single( d => d.DepartmentID == 6) }; try { // Note: No need to add the Course entity into the // ObjectContext because it is automatically done by // relating to an existing Department entity. 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> /// Query the Course entities and the corresponding Department /// entities /// </summary> private static void Query() { using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { foreach (var c in context.Courses) { Console.WriteLine("Course ID:{0}\nTitle:{1}\n" + "Department:{2}", c.CourseID, c.Title, c.Department.Name); } } Console.WriteLine(); }
/// <summary> /// Test method to run the insert and update methods with the /// Independent Association /// </summary> public static void Test() { Console.WriteLine("Inserting new related Course and Department" + " entities by Independent Association..."); // Insert new related entities by Independent Association. InsertNewRelatedEntities(); // Query the database. Query(); Console.WriteLine("Inserting a new Course related to an existing" + " Department entity by Independent Association..."); // Insert a new entity related to an existing entity by // Independent Association. InsertByExistingEntities(); // Query the database. Query(); Console.WriteLine("Updating an existing Course entity " + "(only its regular properties)..."); Course course = null; using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { // Retrieve an existing Course entity. // Note: The Single method is new in EF 4.0. course = context.Courses.Single(c => c.CourseID == 5002); // Modify the Title property of the Course entity. course.Title = "Data Structures"; // Set the relationship by Independent Association. course.Department = context.Departments.Single( d => d.DepartmentID == 5); } // Update an existing Course entity. UpdateExistingEntities(course); // Query the database. Query(); }
/// <summary> /// 测试Independent Association的插入和更新方法。 /// </summary> public static void Test() { Console.WriteLine("Independent Association下插入一条新的Course和Department关联实体..."); // Independent Association插入新的关联实体。 InsertNewRelatedEntities(); // 查询数据库。 Query(); Console.WriteLine("Independent Association下插入一条新的Course并将其关联到一个已存在的Department实体..."); // Independent Association下插入一个新实体并关联到已存在实体。 InsertByExistingEntities(); // 查询数据库。 Query(); Console.WriteLine("更新一个已存在的Course实体和与它关联的Department实体" + "(只有常规属性)..."); Course course = null; using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { // 获取一个已存在的Course实体。 // 注意: 此方法是EF 4.0中的新方法。 course = context.Courses.Single(c => c.CourseID == 5002); // 修改Course实体的Title属性。 course.Title = "Data Structures"; // 在Independent Association条件下设置关系。 course.Department = context.Departments.Single( d => d.DepartmentID == 5); } // 更新一个已存在的Course实体。 UpdateExistingEntities(course); // 查询数据库。 Query(); }
/// <summary> /// Insert a new Course and its Department entity by Independent /// Association /// </summary> private static void InsertNewRelatedEntities() { using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { // Create a new Department entity. Department department = new Department() { DepartmentID = 6, Name = "Software Engineering", Budget = 300000, StartDate = DateTime.Now }; // Create a new Course entity. Course course = new Course() { CourseID = 6001, Title = "Object Oriented", Credits = 4, StatusID = true, // Set the navigation property. Department = department }; try { // Note: Only need to add one entity because the // relationship and related entity will be added // automatically. context.AddToCourses(course); context.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
/// <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> /// 更新一个已存在的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> /// 查询Course实体和对应的Department实体。 /// </summary> private static void Query() { using (IndependentAssociationEntities context = new IndependentAssociationEntities()) { foreach (var c in context.Courses) { Console.WriteLine("Course ID:{0}\nTitle:{1}\n" + "Department:{2}", c.CourseID, c.Title, c.Department.Name); } } Console.WriteLine(); }