Esempio n. 1
0
        private static void SaveDestinationAndLodgings(
            Destination destination,
            List <Lodging> deletedLodgings)
        {
            // TODO: Ensure only Destinations & Lodgings are passed in
            using (var context = new BreakAwayContext())
            {
                context.Destinations.Add(destination);

                if (destination.DestinationId > 0)
                {
                    context.Entry(destination).State = EntityState.Modified;
                }

                foreach (var lodging in destination.Lodgings)
                {
                    if (lodging.LodgingId > 0)
                    {
                        context.Entry(lodging).State = EntityState.Modified;
                    }
                }

                foreach (var lodging in deletedLodgings)
                {
                    context.Entry(lodging).State = EntityState.Deleted;
                }

                context.SaveChanges();
            }
        }
        private static void AddSimpleGraph()
        {
            var essex = new Destination
            {
                Name     = "Essex, Vermont",
                Lodgings = new List <Lodging>
                {
                    new Lodging {
                        Name = "Big Essex Hotel"
                    },
                    new Lodging {
                        Name = "Essex Junction B&B"
                    },
                }
            };

            using (var context = new BreakAwayContext())
            {
                context.Destinations.Add(essex);
                Console.WriteLine("Essex Destination: {0}", context.Entry(essex).State);

                foreach (var lodging in essex.Lodgings)
                {
                    Console.WriteLine("{0}: {1}",
                                      lodging.Name,
                                      context.Entry(lodging).State);
                }
                context.SaveChanges();
            }
        }
Esempio n. 3
0
        private static void DeleteDestinationInMemoryAndDbCascade()
        {
            int destinationId;

            using (var context = new BreakAwayContext())
            {
                var destination = new Destination
                {
                    Name     = "Sample Destination",
                    Lodgings = new List <Lodging>
                    {
                        new Lodging {
                            Name = "Lodging One"
                        },
                        new Lodging {
                            Name = "Lodging Two"
                        }
                    }
                };
                context.Destinations.Add(destination);
                context.SaveChanges();
                destinationId = destination.DestinationId;
            }
            using (var context = new BreakAwayContext())
            {
                var destination = context.Destinations
                                  .Include("Lodgings")
                                  .Single(d => d.DestinationId == destinationId);
                var aLodging = destination.Lodgings.FirstOrDefault();
                context.Destinations.Remove(destination);
                Console.WriteLine("State of one Lodging: {0}",
                                  context.Entry(aLodging).State.ToString());
                context.SaveChanges();
            }
        }
        private static void CreatingNewProxies()
        {
            using (var context = new BreakAwayContext())
            {
                var nonProxy = new Destination();
                nonProxy.Name     = "Non-proxy Destination";
                nonProxy.Lodgings = new List <Lodging>();

                var proxy = context.Destinations.Create();
                proxy.Name = "Proxy Destination";

                context.Destinations.Add(proxy);
                context.Destinations.Add(nonProxy);

                var davesDump = (from l in context.Lodgings
                                 where l.Name == "Dave's Dump"
                                 select l).Single();

                context.Entry(davesDump)
                .Reference(l => l.Destination)
                .Load();

                Console.WriteLine("Before changes: {0}", davesDump.Destination.Name);

                nonProxy.Lodgings.Add(davesDump);
                Console.WriteLine("Added to non-proxy destination: {0}", davesDump.Destination.Name);

                proxy.Lodgings.Add(davesDump);
                Console.WriteLine("Added to proxy destination: {0}", davesDump.Destination.Name);
            }
        }
        private static void ManualDetectChanges()
        {
            using (var context = new BreakAwayContext())
            {
                context.Configuration.AutoDetectChangesEnabled = false;

                var reef = (from d in context.Destinations
                            where d.Name == "Great Barrier Reef"
                            select d).Single();

                reef.Description = "The world's largest reef!";

                Console.WriteLine("Before DetectChanges: {0}", context.Entry(reef).State);
                context.ChangeTracker.DetectChanges();
                Console.WriteLine("After DetectChanges: {0}", context.Entry(reef).State);
            }
        }
 private static void UpdateLodging(Lodging lodging)
 {
     using (var context = new BreakAwayContext())
     {
         context.Entry(lodging).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Esempio n. 7
0
 private static void UpdateDestination(Destination destination)
 {
     using (var context = new BreakAwayContext())
     {
         context.Entry(destination).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Esempio n. 8
0
 private static void AttachDestination(Destination destination)
 {
     using (var context = new BreakAwayContext())
     {
         context.Entry(destination).State = EntityState.Unchanged;
         context.SaveChanges();
     }
 }
Esempio n. 9
0
        /// <summary>
        /// 这个功能演示联级删除 Annotation
        /// </summary>
        static void DeleteDestinationInMemoryAndDbCascade()
        {
            Guid destinationId;

            using (var context = new BreakAwayContext())
            {
                var destination = new AnnotationModel.Destination
                {
                    Name    = "Sample Destination",
                    Address = new AnnotationModel.Address
                    {
                        City          = "City",
                        StreetAddress = "StreetAddress",
                        State         = "State",
                        ZipCode       = "ZipCode"
                    },
                    Info = new AnnotationModel.PersonalInfo
                    {
                        DietryRestrictions = "DietryRestrictions",
                        Height             = new AnnotationModel.Measurement
                        {
                            Reading = 0.1M,
                            Units   = "0.2"
                        },
                        Width = new AnnotationModel.Measurement
                        {
                            Reading = 1.1M,
                            Units   = "1.2"
                        }
                    },
                    Lodgings = new List <AnnotationModel.Lodging>
                    {
                        new AnnotationModel.Lodging
                        {
                            Name = "Lodging One"
                        },
                        new AnnotationModel.Lodging
                        {
                            Name = "Lodging Two"
                        }
                    }
                };

                context.Destinations.Add(destination);
                context.SaveChanges();
                destinationId = destination.DestinationId;
            }
            using (var context = new BreakAwayContext())
            {
                var destination = context.Destinations.Include("Lodgings").Single(d => d.DestinationId == destinationId);
                var aLodging    = destination.Lodgings.FirstOrDefault();
                context.Destinations.Remove(destination);
                Console.WriteLine("State of one Lodging: {0}", context.Entry(aLodging).State.ToString());
                context.SaveChanges();
            }
        }
Esempio n. 10
0
 public ActionResult Edit([Bind(Include = "LodgingId,Name,Owner,IsResort,MilesFromNearestAirport,LocationId")] Lodging lodging)
 {
     if (ModelState.IsValid)
     {
         db.Entry(lodging).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(lodging));
 }
Esempio n. 11
0
 public ActionResult Edit([Bind(Include = "Identifier,StartDate,EndDate,CostUSD,RowVersion")] Trip trip)
 {
     if (ModelState.IsValid)
     {
         db.Entry(trip).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(trip));
 }
Esempio n. 12
0
 public ActionResult Edit([Bind(Include = "SocialSecurityNumber,FirstName,LastName,RowVersion,Info,Address")] Person person)
 {
     if (ModelState.IsValid)
     {
         db.Entry(person).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(person));
 }
Esempio n. 13
0
 public ActionResult Edit([Bind(Include = "DestinationId,Name,Country,Description,Photo")] Destination destination)
 {
     if (ModelState.IsValid)
     {
         db.Entry(destination).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(destination));
 }
Esempio n. 14
0
 // Auslesen der Validationsmeldungen die durch eine Entity produziert werden.
 private static void ConsoleValidationResult(object entity)
 {
     using (var context = new BreakAwayContext())
     {
         var result = context.Entry(entity).GetValidationResult();
         foreach (DbValidationError error in result.ValidationErrors)
         {
             Console.WriteLine(error.ErrorMessage);
         }
     }
 }
 private static void DeleteDestination(int destinationId)
 {
     using (var context = new BreakAwayContext())
     {
         var destination = new Destination {
             DestinationId = destinationId
         };
         context.Entry(destination).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
Esempio n. 16
0
 public ActionResult Edit([Bind(Include = "LodgingId,Name,Owner,IsResort,MilesFromNearestAirport,DestinationId,Entertainment,Activities")] Resort resort)
 {
     if (ModelState.IsValid)
     {
         db.Entry(resort).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.DestinationId = new SelectList(db.Destinations, "DestinationId", "Name", resort.DestinationId);
     return(View(resort));
 }
Esempio n. 17
0
        private static void PrintState()
        {
            using (var context = new BreakAwayContext())
            {
                var canyon = (from d in context.Destinations
                              where d.Name == "Grand Canyon"
                              select d).Single();

                DbEntityEntry <Destination> entry = context.Entry(canyon);

                Console.WriteLine("Before Edit: {0}", entry.State);
                canyon.TravelWarnings = "Take lots of water.";
                Console.WriteLine("After Edit: {0}", entry.State);
            }
        }
        private static void TestIsLoaded()
        {
            using (var context = new BreakAwayContext())
            {
                var canyon = (from d in context.Destinations
                              where d.Name == "Grand Canyon"
                              select d).Single();

                var entry = context.Entry(canyon);

                Console.WriteLine("Before Load: {0}", entry.Collection(d => d.Lodgings).IsLoaded);

                entry.Collection(d => d.Lodgings).Load();
                Console.WriteLine("After Load: {0}", entry.Collection(d => d.Lodgings).IsLoaded);
            }
        }
        private static void QueryLodgingCount()
        {
            using (var context = new BreakAwayContext())
            {
                var canyonQuery = from d in context.Destinations
                                  where d.Name == "Grand Canyon"
                                  select d;

                var canyon = canyonQuery.Single();

                var lodgingQuery = context.Entry(canyon)
                                   .Collection(d => d.Lodgings)
                                   .Query();

                var lodgingCount = lodgingQuery.Count();
                Console.WriteLine("Lodging at Grand Canyon: " + lodgingCount);
            }
        }
Esempio n. 20
0
        // Explicit Loading
        private static void TestExplicitLoading()
        {
            using (var context = new BreakAwayContext())
            {
                var query = from d in context.Destinations
                            where d.Name == "Grand Canyon"
                            select d;

                var canyon = query.SingleOrDefault();

                context.Entry(canyon).Collection(d => d.Lodgings).Load();

                System.Console.WriteLine("Grabd Canyon Lodging");
                foreach (var lodging in canyon.Lodgings)
                {
                    System.Console.WriteLine(lodging.Name);
                }
            }
        }
Esempio n. 21
0
        private static void PrintChangeTrackingInfo(BreakAwayContext context,
                                                    Lodging entity)
        {
            var entry = context.Entry(entity);

            if (entry.State == EntityState.Deleted)
            {
                Console.WriteLine("\nCurrent Values:");
                PrintPropertyValues(entry.CurrentValues);
            }

            if (entry.State == EntityState.Added)
            {
                Console.WriteLine("\nOriginal Values:");
                PrintPropertyValues(entry.OriginalValues);

                Console.WriteLine("\nDatabase Values:");
                PrintPropertyValues(entry.GetDatabaseValues());
            }
        }
        public void ValidationDetectsMissingPhotoInPerson()
        {
            var person = new Person
            {
                FirstName = "Mikael",
                LastName  = "Eliasson"
            };

            DbEntityValidationResult result;

            using (var context = new BreakAwayContext())
            {
                result = context.Entry(person).GetValidationResult();
            }

            Assert.IsFalse(result.IsValid);
            Assert.IsTrue(result.ValidationErrors
                          .Any(v => v.ErrorMessage.ToLower()
                               .Contains("photo field is required")));
        }
        private static void DetectRelationshipChanges()
        {
            using (var context = new BreakAwayContext())
            {
                var hawaii = (from d in context.Destinations
                              where d.Name == "Hawaii"
                              select d).Single();

                var davesDump = (from l in context.Lodgings
                                 where l.Name == "Dave's Dump"
                                 select l).Single();

                context.Entry(davesDump)
                .Reference(l => l.Destination)
                .Load();

                hawaii.Lodgings.Add(davesDump);
                Console.WriteLine("Before DetectChanges: {0}", davesDump.Destination.Name);
                context.ChangeTracker.DetectChanges();
                Console.WriteLine("After DetectChanges: {0}", davesDump.Destination.Name);
            }
        }
Esempio n. 24
0
        /* Chapter 6 Validation API
         *
         * Validating a Single Object on Demand with GetValidationResult
         * ------------------------------------------------------------
         *
         * In Destionation Class
         * -------------------------------------------
         *  [MaxLength(10)]
         *  public string LastName { get; set; }
         * ---------------------------------------------
         *
         *  Now let’s see what happens when we set the length to a string with more than ten
         *  characters. GetValidationResult allows you to explicitly validate a single entity. It returns
         *  a ValidationResult type that contains three important members. We’ll focus on
         *  just one of those for now, the IsValid property, which is a Boolean that indicates if the
         *  instance passed its validation rules. Let’s use that to validate a Person instance.
         *
         *
         *
         */

        private static void ValidateNewPerson()
        {
            var person = new Person
            {
                FirstName = "Julie",
                LastName  = "Lerman",
                Photo     = new PersonPhoto {
                    Photo = new Byte[] { 0 }
                }
            };

            using (var context = new BreakAwayContext())
            {
                if (context.Entry(person).GetValidationResult().IsValid)
                {
                    Console.WriteLine("Person is Valid");
                }
                else
                {
                    Console.WriteLine("Person is Invalid");
                }
            }
        }
        private static void QueryLodgingDistance()
        {
            using (var context = new BreakAwayContext())
            {
                var canyonQuery = from d in context.Destinations
                                  where d.Name == "Grand Canyon"
                                  select d;

                var canyon = canyonQuery.Single();

                var lodgingQuery = context.Entry(canyon)
                                   .Collection(d => d.Lodgings)
                                   .Query();

                var distanceQuery = from l in lodgingQuery
                                    where l.MilesFromNearestAirport <= 10
                                    select l;

                foreach (var lodging in distanceQuery)
                {
                    Console.WriteLine(lodging.Name);
                }
            }
        }