/// <summary> /// Retrieve Complex Type value by Function Import /// </summary> static void GetComplexTypeByFunctionImport() { using (ComplexTypeEntities context = new ComplexTypeEntities()) { // Retrieve the PersonName Complex Type object by the // Function Import GetPersonNameByPersonID. var personNames = context.GetPersonNameByPersonID(40); // Display the PersonName Complex Type object. // Note: The ToString() method of the PersonName class has // been overrided. Console.WriteLine("Person name whose ID is 40: {0}", personNames.Single()); } }
/// <summary> /// Query Person entity with Complex Types PersonAddress and /// PersonDate /// </summary> static void QueryEntityWithComplexTypeProperties() { using (ComplexTypeEntities context = new ComplexTypeEntities()) { // Get the Person entity whose PersonID is 40. // Note: The Single method is new in EF 4.0. Person person = context.People.Single(p => p.PersonID == 40); // Display the Person information and Complex Type properties. Console.WriteLine( "Person:{0} {1}\n" + "EnrollmentDate:{2}\n" + "Address:{3}\n" + "Postcode:{4}", person.FirstName, person.LastName, person.PersonDate.EnrollmentDate, person.PersonAddress.Address, person.PersonAddress.Postcode); } }
/// <summary> /// Insert Person entity with Complex Types PersonAddress and /// PersonDate /// </summary> static void InsertEntityWithComplexTypeProperties() { using (ComplexTypeEntities context = new ComplexTypeEntities()) { // Create a new PersonAddress Complex Type object. PersonAddress personAddress = new PersonAddress() { Address = "Shanghai, China", Postcode = "200000" }; // Create a new PersonDate Complex Type object. PersonDate personDate = new PersonDate() { EnrollmentDate = DateTime.Now }; // Create a new Person entity with the two Complex Types. Person person = new Person() { PersonID = 40, FirstName = "Lingzhi", LastName = "Sun", // Set PersonDate Complex Type property. PersonDate = personDate, // Set the PersonAddress Complex Type property. PersonAddress = personAddress }; try { context.AddToPeople(person); context.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }