Example #1
0
        // Insert new person with new personAddress
        public static void InsertPersonWithPersonAddress()
        {
            using (EFO2OEntities context = new EFO2OEntities())
            {
                Person person = new Person()
                {
                    FirstName = "Lingzhi",
                    LastName  = "Sun"
                };

                // The PersonID in PersonAddress will be 27 because it
                // depends on person.PersonID
                PersonAddress personAddress = new PersonAddress()
                {
                    PersonID = 100,
                    Address  = "Shanghai",
                    Postcode = "200021"
                };

                // Set navigation property (one-to-one)
                person.PersonAddress = personAddress;

                context.AddToPerson(person);

                try
                {
                    Console.WriteLine("Inserting a person with "
                                      + "person address");

                    context.SaveChanges();

                    Query();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Example #2
0
        // 使用personAddress插入新的person
        public static void InsertPersonWithPersonAddress()
        {
            using (EFO2OEntities context = new EFO2OEntities())
            {
                Person person = new Person()
                {
                    FirstName = "Lingzhi",
                    LastName  = "Sun"
                };

                // PersonAddress中的PersonID将是27因为它依赖于person.PersonID
                PersonAddress personAddress = new PersonAddress()
                {
                    PersonID = 100,
                    Address  = "Shanghai",
                    Postcode = "200021"
                };

                // 设置navigation属性 (一对一)
                person.PersonAddress = personAddress;

                context.AddToPerson(person);

                try
                {
                    Console.WriteLine("Inserting a person with "
                                      + "person address");

                    context.SaveChanges();

                    Query();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Example #3
0
        // 使用personAddress插入新的person
        public static void InsertPersonWithPersonAddress()
        {
            using (EFO2OEntities context = new EFO2OEntities())
            {
                Person person = new Person()
                {
                    FirstName = "Lingzhi",
                    LastName ="Sun"
                };

                // PersonAddress中的PersonID将是27因为它依赖于person.PersonID
                PersonAddress personAddress = new PersonAddress()
                {
                    PersonID = 100,
                    Address = "Shanghai",
                    Postcode = "200021"
                };

                // 设置navigation属性 (一对一)
                person.PersonAddress = personAddress;

                context.AddToPerson(person);

                try
                {
                    Console.WriteLine("Inserting a person with "
                        + "person address");

                    context.SaveChanges();

                    Query();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }