Exemple #1
0
        static void Main(string[] args)
        {
            //if (new String("Bob".ToCharArray()) == new String("Bob".ToCharArray()))
            //    Console.WriteLine("Really?");

            using (var db = new SchoolDbContext4())
            {
                //var david = db.People.Include(p=>p.Cars) //loads Cars despite lazy loading
                //    .FirstOrDefault(p => p.First.StartsWith("Dav"));
                //if (david == null)
                //{
                //    // log
                //}
                //david.Cars.Add(new Car {Color = Color.Green, Make = "Ferrarri", Model = "912"});
                //david.First = "Dave"; // EF tracks changes and will save them

                var c = new Car()
                {
                    Color = Color.Lavender
                };

                db.Cars.Add(c);

                db.SaveChanges();

                // Console.WriteLine(david);
            }
        }
Exemple #2
0
        static void AddPerson(Person p2)
        {
            Person p = new Person
            {
                First = "David",
                Last  = "Langstein",
            };

            p.Cars = new List <Car>();
            p.Cars.Add(new Car
            {
                Color = Color.Algerian,
                Make  = "Mazda",
                Model = "911"
            });
            using (var db = new SchoolDbContext4())
            {
                db.People.Add(p);
                db.SaveChanges();
            }
        }