Пример #1
0
        // Update one existing person
        public static void UpdatePerson()
        {
            using (EFO2OEntities context = new EFO2OEntities())
            {
                Person person = new Person();

                person.PersonID = 1;

                context.AttachTo("Person", person);

                person.LastName = "Chen";

                // Set the foreign key as null
                person.PersonAddress = null;

                try
                {
                    Console.WriteLine("Modifying Person 1's LastName to {0}"
                                      + ", and PersonAddress to null", person.LastName);

                    context.SaveChanges();

                    Query();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Пример #2
0
        // Get all the people with their addresses
        public static void Query()
        {
            using (EFO2OEntities context = new EFO2OEntities())
            {
                var query = from p in context.Person.Include("PersonAddress")
                            select p;

                Console.WriteLine("People with their addresses:");

                foreach (Person p in query)
                {
                    Console.WriteLine("{0} {1}", p.PersonID, p.LastName);

                    if (p.PersonAddress != null)
                    {
                        Console.WriteLine("   {0}", p.PersonAddress.Address);
                    }
                }

                Console.WriteLine();
            }
        }
Пример #3
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);
                }
            }
        }
Пример #4
0
        // 获取带有addresses的所有people
        public static void Query()
        {
            using (EFO2OEntities context = new EFO2OEntities())
            {
                var query = from p in context.Person.Include("PersonAddress")
                            select p;

                Console.WriteLine("People with their addresses:");

                foreach (Person p in query)
                {
                    Console.WriteLine("{0} {1}", p.PersonID, p.LastName);

                    if (p.PersonAddress != null)
                    {
                        Console.WriteLine("   {0}", p.PersonAddress.Address);
                    }
                }

                Console.WriteLine();
            }
        }
Пример #5
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);
                }
            }
        }
Пример #6
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);
                }
            }
        }
Пример #7
0
        // 更新一个现存的person
        public static void UpdatePerson()
        {
            using (EFO2OEntities context = new EFO2OEntities())
            {
                Person person = new Person();

                person.PersonID = 1;

                context.AttachTo("Person", person);

                person.LastName = "Chen";

                // 将外键设置为空
                person.PersonAddress = null;

                try
                {
                    Console.WriteLine("Modifying Person 1's LastName to {0}"
                        + ", and PersonAddress to null", person.LastName);

                    context.SaveChanges();

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