コード例 #1
0
        public void AddPerson()
        {
            //using (CarOwnersContext context = new CarOwnersContext())
            //{
            //    context.People.Add(new Person { Age = 3, Name = "Pesho" });
            //    context.SaveChanges();
            //}

            context.People.Add(new Person {
                Age = 3, Name = "Pesho", Nickname = "Kartofa", Height = 190
            });
            context.SaveChanges();
        }
コード例 #2
0
ファイル: CarsController.cs プロジェクト: eaallen/blowoutEli
        public ActionResult AddOwner(CarsOwner carsowner)
        {
            if (ModelState.IsValid)
            {
                var entity = new Owner()
                {
                    ownerFirstName = carsowner.owner.ownerFirstName, ownerLastName = carsowner.owner.ownerLastName
                };

                db.owner.Add(entity);

                db.SaveChanges();

                int newOwner = (int)entity.ownerID;

                using (db)
                {
                    // make sure you have the right column/variable used here
                    var cars = db.car.FirstOrDefault(x => x.carID == carsowner.cars.carID);

                    if (cars == null)
                    {
                        throw new Exception("Invalid id: " + carsowner.cars.carID);
                    }

                    // this variable is tracked by the db context
                    cars.ownerID = newOwner;

                    db.SaveChanges();
                }

                return(RedirectToAction("Index"));
            }

            return(View(carsowner));
        }
コード例 #3
0
 public void Save()
 {
     db.SaveChanges();
 }