Example #1
0
        /// <summary>
        /// Add records for bands and musicians to database.
        /// </summary>
        static void SeedDatabase()
        {
            Console.WriteLine("Attempting to seed database...");
            try
            {
                // check if data exists
                using (var db = new DB_Bands())
                {
                    var repo = new MusicianRepository(db);
                    if (repo.GetAll().Count > 0)
                    {
                        Console.WriteLine("Data Exists! Retrieving data... ");
                        Console.WriteLine("");
                        return;
                    }
                }

                // Seeding database - make sure the App.config connection string is targeting a valid SQL Server instance.
                Console.WriteLine("Seeding database... ");

                using (var db = new DB_Bands())
                {
                    var musicians    = BandDataLayer.GetMusicians();
                    var musicianRepo = new MusicianRepository(db);
                    foreach (var musician in musicians)
                    {
                        musicianRepo.Add(musician);
                    }
                    musicianRepo.Save();

                    // let's check if all data was stored
                    var seedBands = BandDataLayer.GetBands();
                    var bandRepo  = new GenericRepository <Band>(db);
                    var dbbands   = bandRepo.GetAll().Select(y => y.Name).ToList();
                    foreach (var band in seedBands)
                    {
                        if (!dbbands.Contains(band.Name))
                        {
                            bandRepo.Add(band);
                        }
                    }
                    bandRepo.Save();

                    Console.WriteLine("Data Stored to Database!");
                    Console.WriteLine("");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occured while trying to seed the database: " + ex.Message);
            }
        }
Example #2
0
        private MusicianRepository ReadMusicians(XElement xmlElement, XNamespace xmlNamespace)
        {
            MusicianRepository musicianRepository = new MusicianRepository();

            IEnumerable <Musician> musicians = from musician in xmlElement.Descendants(xmlNamespace + MusicXmlConstants.MUSICIAN_NODE)
                                               select new Musician()
            {
                Id   = musician.Attribute(MusicXmlConstants.MUSICIAN_ID).Value,
                Name = musician.Element(xmlNamespace + MusicXmlConstants.MUSICIAN_NAME).Value
            };

            foreach (Musician musician in musicians)
            {
                musicianRepository.Add(musician);
            }

            return(musicianRepository);
        }