示例#1
0
        /// <summary>
        /// Process the changes to the Interest database as specified in the change requests
        /// </summary>
        /// <param name="person">Person to associate the interest with</param>
        /// <param name="changeRequests">Set of changes of the form 'verb:interest' where verb is one of 'Add' or 'Del' for
        /// add or delete repectively, and the interest is the interest to add or delete.</param>
        /// <returns>Number of entries in the change list that were processed.</returns>
        public static async Task <int> ProcessChanges(Person person, List <string> changeRequests, PeopleSearchContext context)
        {
            int numberChanged = 0;

            Person dbPerson = context.Person.Where(entry => entry.PersonId == person.PersonId).Single();

            foreach (string change in changeRequests)
            {
                string[] parts = change.Split(":");

                if (parts.Length == 2)
                {
                    string verb     = parts[0];
                    string interest = parts[1];

                    switch (verb)
                    {
                    case "Add":
                        context.Interest.Add(new Interest
                        {
                            Person      = dbPerson,
                            TheInterest = interest
                        });
                        numberChanged++;
                        break;

                    case "Del":

                        context.Interest.RemoveRange(
                            context.Interest.Where(entry => entry.Person == dbPerson &&
                                                   entry.TheInterest.ToLower() == interest.ToLower()));
                        numberChanged++;
                        break;

                    default:

                        MessageHandler.Error($"Invalid verb ({verb}) in Interest change list");
                        break;
                    }
                }
            } // FOREACH entry in the change requests list

            await context.SaveChangesAsync();

            return(numberChanged);
        }
        /// <summary>
        /// Seed the Database
        /// </summary>
        /// <param name="serviceProvider"></param>
        public static void Initialize(DbContextOptions <ImageContext> imageDbOptions,
                                      DbContextOptions <PeopleSearchContext> peopleDbContext)
        {
            Image imageUnknown;
            Image imageRichard;
            Image imageTrent;

            // Create the Image Database Entries
            // Using the Context object
            using (var context = new ImageContext(imageDbOptions))
            {
                // Check for anybody already in the database
                if (context.Image.Any())
                {
                    // Database already seeded, exit
                    return;
                }

                // Create Images

                imageUnknown = new Image(@".\SeedData\Unknown.jpg")
                {
                    Id = "0"
                };
                imageRichard = new Image(@".\SeedData\Richard.jpg")
                {
                    NumberPeopleAssignedTo = 1
                };
                imageTrent = new Image(@".\SeedData\Trent.jpg")
                {
                    NumberPeopleAssignedTo = 1
                };

                context.Image.AddRange(imageUnknown, imageRichard, imageTrent);
                context.SaveChanges();
            }

            // Using the Context object
            using (var context = new PeopleSearchContext(peopleDbContext))
            {
                // Check for anybody already in the database
                if (context.Person.Any())
                {
                    // Database already seeded, exit
                    return;
                }

                Person richard = new Person
                {
                    FirstName        = "Richard",
                    LastName         = "Bousman",
                    BirthDate        = new DateTime(1990, 10, 17),
                    ImageGUID        = imageRichard.Id,
                    AddressLine1     = "2880 Sandestin",
                    AddressLine2     = "",
                    City             = "Reno",
                    StateOrTerritory = "Nevada",
                    ZipCode          = "89523",
                    Country          = "USA"
                };

                Person trent = new Person
                {
                    FirstName        = "Trent",
                    LastName         = "Wignall",
                    BirthDate        = new DateTime(1989, 6, 19),
                    ImageGUID        = imageTrent.Id,
                    AddressLine1     = "Corner Office",
                    AddressLine2     = "3165 Millrock Dr",
                    City             = "Salt Lake City",
                    StateOrTerritory = "Utah",
                    ZipCode          = "84121",
                    Country          = "USA"
                };

                Person fred = new Person
                {
                    FirstName        = "Fred",
                    LastName         = "Flinestone",
                    BirthDate        = new DateTime(0001, 5, 4),
                    ImageGUID        = imageUnknown.Id,
                    AddressLine1     = "245 Rock Blvd",
                    AddressLine2     = "",
                    City             = "Bedrock",
                    StateOrTerritory = "Stonehenge",
                    ZipCode          = "00000",
                    Country          = "Prehistoric"
                };

                // Add some People
                context.Person.AddRange(richard, trent, fred);

                context.Interest.AddRange(

                    new Interest
                {
                    Person      = richard,
                    TheInterest = "Programming"
                },

                    new Interest
                {
                    Person      = richard,
                    TheInterest = "Baseball"
                },

                    new Interest
                {
                    Person      = trent,
                    TheInterest = "Personnel"
                },

                    new Interest
                {
                    Person      = trent,
                    TheInterest = "Cycling"
                },

                    new Interest
                {
                    Person      = fred,
                    TheInterest = "Rock Climbing"
                }
                    );

                // Save new entries to database, assigning personId's
                context.SaveChanges();
            }
        }