Create seed data:
We need to create ModelBuilderExtensions
class that extend the ModelBuilder
functionalities by adding seed method that initialize database with data, this class wil be created inside DashBoardWebApp.Infrastructure/Data/Config:
public static class ModelBuilderExtensions
{
public static void Seed(this ModelBuilder modelBuilder)
{
List<Profession> professions = new List<Profession>()
{
new Profession() { Id = 1, Name = "Software Developer"},
new Profession() { Id = 2, Name = "Dentist"},
new Profession() { Id = 3, Name = "Physician" }
};
modelBuilder.Entity<Profession>().HasData(
professions
);
List<User> users = new List<User>()
{
new User() { Id=1, FirstName = "O.Nasri 1", Age = 30, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2019, 01, 01) },
new User() { Id=2, FirstName = "O.Nasri 2 ", Age = 31, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2019, 01, 02) },
new User() { Id=3, FirstName = "O.Nasri 3", Age = 32, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2019, 01, 02) },
new User() { Id=4, FirstName = "O.Nasri 4", Age = 33, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2019, 01, 04) },
new User() { Id=5, FirstName = "O.Nasri 4", Age = 33, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2019, 02, 05) },
new User() { Id=6, FirstName = "Sonia 1", Age = 20, Gender = "Female",
ProfessionId = 2, CreatedAt = new DateTime(2019, 04, 01) } ,
new User() { Id=7, FirstName = "Sonia 2", Age = 20, Gender = "Female",
ProfessionId = 2, CreatedAt = new DateTime(2019, 04, 02) } ,
new User() { Id=8, FirstName = "Sonia 3", Age = 20, Gender = "Female",
ProfessionId = 2, CreatedAt = new DateTime(2019, 05, 03) } ,
new User() { Id=9, FirstName = "Sonia 4", Age = 20, Gender = "Female",
ProfessionId = 2, CreatedAt = new DateTime(2019, 05, 04) } ,
new User() { Id=10, FirstName = "O.Nasri 1", Age = 30, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2020, 01, 01) },
new User() { Id=11, FirstName = "O.Nasri 2 ", Age = 31, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2020, 01, 02) },
new User() { Id=12, FirstName = "O.Nasri 3", Age = 32, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2020, 01, 02) },
new User() { Id=13, FirstName = "O.Nasri 4", Age = 33, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2020, 01, 04) },
new User() { Id=14, FirstName = "O.Nasri 4", Age = 33, Gender = "Male",
ProfessionId = 1, CreatedAt = new DateTime(2020, 01, 05) },
new User() { Id=15, FirstName = "Thomas 1", Age = 41, Gender = "Male",
ProfessionId = 2, CreatedAt = new DateTime(2020, 03, 01) } ,
new User() { Id=16, FirstName = "Thomas 2", Age = 42, Gender = "Male",
ProfessionId = 2, CreatedAt = new DateTime(2020, 03, 02) } ,
new User() { Id=17, FirstName = "Thomas 3", Age = 43, Gender = "Male",
ProfessionId = 2, CreatedAt = new DateTime(2020, 03, 03) } ,
new User() { Id=18, FirstName = "Thomas 4", Age = 44, Gender = "Male",
ProfessionId = 2, CreatedAt = new DateTime(2020, 03, 04) } ,
new User() { Id=19, FirstName = "Christophe 1", Age = 25, Gender = "Male",
ProfessionId = 3, CreatedAt = new DateTime(2020, 05, 01) },
new User() { Id=20, FirstName = "Christophe 2", Age = 26, Gender = "Male",
ProfessionId = 3, CreatedAt = new DateTime(2020, 05, 02) },
new User() { Id=21, FirstName = "Christophe 3", Age = 27, Gender = "Male",
ProfessionId = 3, CreatedAt = new DateTime(2020, 05, 03)},
new User() { Id=22, FirstName = "Linda 1", Age = 18, Gender = "Female",
ProfessionId = 1, CreatedAt = new DateTime(2020, 06, 01) },
new User() { Id=23, FirstName = "Linda 2 ", Age = 19, Gender = "Female",
ProfessionId = 1, CreatedAt = new DateTime(2020, 06, 02) },
new User() { Id=24, FirstName = "Linda 3", Age = 20, Gender = "Female",
ProfessionId = 1, CreatedAt = new DateTime(2020, 06, 02) },
new User() { Id=25, FirstName = "Linda 4", Age = 21, Gender = "Female",
ProfessionId = 1, CreatedAt = new DateTime(2020, 06, 04) },
new User() { Id=26, FirstName = "Linda 4", Age = 22, Gender = "Female",
ProfessionId = 1, CreatedAt = new DateTime(2020, 06, 05) },
new User() { Id=27, FirstName = "Dalida 1", Age = 40, Gender = "Female",
ProfessionId = 2, CreatedAt = new DateTime(2020, 09, 06) } ,
new User() { Id=28, FirstName = "Dalida 2", Age = 41, Gender = "Female",
ProfessionId = 2, CreatedAt = new DateTime(2020, 09, 07) } ,
new User() { Id=29, FirstName = "Dalida 3", Age = 42, Gender = "Female",
ProfessionId = 2, CreatedAt = new DateTime(2020, 09, 08) } ,
new User() { Id=30, FirstName = "Dalida 4", Age = 43, Gender = "Female",
ProfessionId = 2, CreatedAt = new DateTime(2020, 09, 09) } ,
};
modelBuilder.Entity<User>().HasData(
users
);
}
}