// Insert and Query PersonWithPersonDetail
        public static void InsertQueryPersonWithPersonDetail()
        {
            // Create the new Person entity
            Person person = new Person();

            person.FirstName = "Lopez";
            person.LastName  = "Typot";

            // Create the new PersonDetail entity
            PersonDetail personDetail = new PersonDetail();

            personDetail.PersonCategory = 0;
            personDetail.HireDate       = System.DateTime.Now;

            // Set the PersonDetail to Person
            person.PersonDetail = personDetail;

            // Insert PersonWithPersonDetail
            using (EFTblSplitEntities context = new EFTblSplitEntities())
            {
                context.AddToPerson(person);

                Console.WriteLine("Saving person {0} {1}", person.FirstName,
                                  person.LastName);

                // Note that personDetail.PersonID is the same as
                // person.PersonID. This is why we love the Entity Framework.
                Console.WriteLine("Saving person detail with the same " +
                                  "personID.\n");

                context.SaveChanges();
            }

            // Query PersonWithPersonDetail
            using (EFTblSplitEntities context = new EFTblSplitEntities())
            {
                // Retrieve the newly inserted person
                Person person2 = (from p in context.Person
                                  where p.PersonID == person.PersonID
                                  select p).FirstOrDefault();

                Console.WriteLine(
                    "Retrieved person {0} with person detail",
                    person2.PersonID,
                    person2.PersonDetail);

                person2.PersonDetailReference.Load();

                Console.WriteLine(
                    "Retrieved hiredate for person detail {0}",
                    person2.PersonDetail.HireDate);
            }
        }
Example #2
0
        // 插入和查询PersonWithPersonDetail
        public static void InsertQueryPersonWithPersonDetail()
        {
            // 创建新的Person实体
            Person person = new Person();

            person.FirstName = "Lopez";
            person.LastName  = "Typot";

            // 创建新的PersonDetail实体
            PersonDetail personDetail = new PersonDetail();

            personDetail.PersonCategory = 0;
            personDetail.HireDate       = System.DateTime.Now;

            // 将person中的PersonDetail属性设置成此PersonDetail
            person.PersonDetail = personDetail;

            // 插入PersonWithPersonDetail
            using (EFTblSplitEntities context = new EFTblSplitEntities())
            {
                context.AddToPerson(person);

                Console.WriteLine("Saving person {0} {1}", person.FirstName,
                                  person.LastName);

                // 请注意personDetail.PersonID和person.PersonID是相同的,这就
                // 是我们为什么喜欢Entity Framework的原因.
                Console.WriteLine("Saving person detail with the same " +
                                  "personID.\n");

                context.SaveChanges();
            }

            // 查询 PersonWithPersonDetail
            using (EFTblSplitEntities context = new EFTblSplitEntities())
            {
                // 获取刚插入的person
                Person person2 = (from p in context.Person
                                  where p.PersonID == person.PersonID
                                  select p).FirstOrDefault();

                Console.WriteLine(
                    "Retrieved person {0} with person detail",
                    person2.PersonID,
                    person2.PersonDetail);

                person2.PersonDetailReference.Load();

                Console.WriteLine(
                    "Retrieved hiredate for person detail {0}",
                    person2.PersonDetail.HireDate);
            }
        }
        // 插入和查询PersonWithPersonDetail
        public static void InsertQueryPersonWithPersonDetail()
        {
            // 创建新的Person实体
            Person person = new Person();
            person.FirstName = "Lopez";
            person.LastName = "Typot";

            // 创建新的PersonDetail实体
            PersonDetail personDetail = new PersonDetail();
            personDetail.PersonCategory = 0;
            personDetail.HireDate = System.DateTime.Now;

            // 将person中的PersonDetail属性设置成此PersonDetail
            person.PersonDetail = personDetail;

            // 插入PersonWithPersonDetail
            using (EFTblSplitEntities context = new EFTblSplitEntities())
            {
                context.AddToPerson(person);

                Console.WriteLine("Saving person {0} {1}", person.FirstName,
                    person.LastName);

                // 请注意personDetail.PersonID和person.PersonID是相同的,这就
                // 是我们为什么喜欢Entity Framework的原因.
                Console.WriteLine("Saving person detail with the same " +
                    "personID.\n");

                context.SaveChanges();
            }

            // 查询 PersonWithPersonDetail
            using (EFTblSplitEntities context = new EFTblSplitEntities())
            {
                // 获取刚插入的person
                Person person2 = (from p in context.Person
                                  where p.PersonID == person.PersonID
                                  select p).FirstOrDefault();

                Console.WriteLine(
                    "Retrieved person {0} with person detail",
                    person2.PersonID,
                    person2.PersonDetail);

                person2.PersonDetailReference.Load();

                Console.WriteLine(
                    "Retrieved hiredate for person detail {0}",
                    person2.PersonDetail.HireDate);
            }
        }