public static void Initialize(ConferenceAppContext context)
        {
            var genders   = context.Genders.ToList();
            var roles     = context.ParticipantRoles.ToList();
            var countries = context.Countries.ToList();

            var participantFaker = new Faker <Participant>()
                                   .CustomInstantiator(f => new Participant())
                                   .RuleFor(o => o.FirstName, f => f.Person.FirstName)
                                   .RuleFor(o => o.LastName, f => f.Person.LastName)
                                   .RuleFor(o => o.Email, f => f.Person.Email)
                                   .RuleFor(o => o.DateOfArrival, f => f.Date.Future())
                                   .RuleFor(o => o.DateOfDeparture, f => f.Date.Future())
                                   .RuleFor(o => o.CompanyName, f => f.Company.CompanyName())
                                   .RuleFor(o => o.PositionInCompany, f => f.Person.UserName)
                                   .RuleFor(o => o.Role, f => f.Random.ListItem <ParticipantRole>(roles))
                                   .RuleFor(o => o.Gender, f => f.Random.ListItem <Gender>(genders))
                                   .RuleFor(o => o.BirthDate, f => f.Date.Past(20))
                                   .RuleFor(o => o.Country, f => f.Random.ListItem <Country>(countries));

            var participants = participantFaker.Generate(100);

            foreach (Participant p in participants)
            {
                context.Participants.AddAsync(p);
            }
            context.SaveChanges();
        }
Beispiel #2
0
        // I guess this initializer can be improoved
        public static void Initialize(ConferenceAppContext context)
        {
            context.Database.EnsureCreated();

            if (context.ParticipantRoles.Any())
            {
                return;
            }

            ParticipantRole[] participantRoles = new ParticipantRole[]
            {
                new ParticipantRole {
                    RoleName = "Speaker"
                },
                new ParticipantRole {
                    RoleName = "Administrator"
                },
                new ParticipantRole {
                    RoleName = "Listener"
                },
            };

            foreach (ParticipantRole pr in participantRoles)
            {
                context.ParticipantRoles.Add(pr);
            }
            context.SaveChanges();

            IReadOnlyList <ICountryInfo> allCountryInfo = CountryLoader.CountryInfo;

            foreach (ICountryInfo country in allCountryInfo)
            {
                Country seedCountry = new Country {
                    Name = country.Name, Iso = country.Iso, Iso3 = country.Iso3, IsoNumeric = country.IsoNumeric
                };
                context.Countries.Add(seedCountry);
            }
            context.SaveChanges();

            Gender[] genders = new Gender[]
            {
                new Gender {
                    Value = "Male"
                },
                new Gender {
                    Value = "Female"
                },
            };

            foreach (Gender g in genders)
            {
                context.Genders.Add(g);
            }
            context.SaveChanges();
        }