public ActionResult Create([Bind(Include = "ConditionID,TrailID,Date,ConditionText")] Condition condition)
        {
            if (ModelState.IsValid)
            {
                db.Conditions.Add(condition);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(condition));
        }
        public void ShouldAddNewPerson()
        {
            using (new TransactionScope())
            {
                using (TrailsContext context = new TrailsContext())
                {
                    DbSet<Person> persons = context.Persons;
                    persons.Load();
                    int priorCount = persons.Local.Count;

                    Person newPerson = new Person
                    {
                        FirstName = "testFirstName",
                        LastName = "testLastName",
                        DateOfBirth = DateTime.Today.AddYears(-32),
                        Gender = "M"
                    };

                    // Add the new person to the context
                    Person person = persons.Add(newPerson);

                    // Save the context to the database, causing the database to be updated
                    context.SaveChanges();
                    // At this stage, the database has the new row, so the identity has been incremented and assigned
                    Assert.IsTrue(person.Id > 0);
                    Assert.IsTrue(newPerson.Id > 0);

                    persons.Load();
                    int postCount = persons.Local.Count;

                    Assert.AreEqual(postCount, priorCount + 1);
                }
                // Rollback the transaction, to make the test repeatable
            }
        }
Example #3
0
        public TrailsController(TrailsContext context)
        {
            _context = context;

            if (_context.Trails.Count() == 0)
            {
                // Create a new Trail if collection is empty,
                // which means you can't delete all Trails.
                _context.Trails.Add(new Trail
                {
                    Name   = "Magic Mystery Trail",
                    Status = Trail.TrailStatus.Green
                });

                _context.SaveChanges();
            }
        }
Example #4
0
        public void ShouldAddNewTrip()
        {
            // To turn on DTS on my computer,
            // see http://alexduggleby.com/2008/08/24/msdtc-unavailable-for-sql-express-transactions-or-who-took-my-msdtc-settings-on-vista/
            // or http://geekswithblogs.net/narent/archive/2006/10/09/93544.aspx
               using (new TransactionScope())
            {
                using (TrailsContext context = new TrailsContext())
                {
                    DbSet<Trip> trips = context.Trips;
                    trips.Load();
                    int priorCount = trips.Local.Count;

                    DbSet<Person> persons = context.Persons;
                    persons.Load();

                    Trip newTrip = new Trip
                    {
                        Date = DateTime.Today,
                        TimeTaken = 3.1M,
                        TransportTypeId = 1,
                        TrailId = 1,
                        Persons = new List<Person>{persons.Local.ElementAt(0), persons.Local.ElementAt(2)}
                    };

                    // Add the new trip to the context
                    Trip trip = trips.Add(newTrip);

                    // Save the context to the database, causing the database to be updated
                    context.SaveChanges();
                    // At this stage, the database has the new row, so the identity has been incremented and assigned
                    Assert.IsTrue(trip.Id > 0);
                    Assert.IsTrue(newTrip.Id > 0);

                    trips.Load();
                    int postCount = trips.Local.Count;

                    Assert.AreEqual(postCount, priorCount + 1);
                }
                // Rollback the transaction, to make the test repeatable
            }
        }
        public void ShouldAddNewTrail()
        {
            using (new TransactionScope())
            {
                using (TrailsContext context = new TrailsContext())
                {
                    DbSet<Trail> trails = context.Trails;
                    trails.Load();
                    int priorCount = trails.Local.Count;

                    Trail newTrail = new Trail
                    {
                        Name = "testTrailName",
                        Description = "testDescription",
                        Distance = 32,
                        ElevationGain = 3000M,
                        TrailTypeId = 1,
                        LocationId = 1,
                        DifficultyId = 1
                    };

                    // Add the new trail to the context
                    Trail trail = trails.Add(newTrail);

                    // Save the context to the database, causing the database to be updated
                    context.SaveChanges();
                    // At this stage, the database has the new row, so the identity has been incremented and assigned
                    Assert.IsTrue(trail.Id > 0);
                    Assert.IsTrue(newTrail.Id > 0);

                    trails.Load();
                    int postCount = trails.Local.Count;

                    Assert.AreEqual(postCount, priorCount + 1);
                }
                // Rollback the transaction, to make the test repeatable
            }
        }