filmov
tv
Part 6 How to seed database with test data using entity framework
![preview_player](https://i.ytimg.com/vi/XED_WgDmK4c/sddefault.jpg)
Показать описание
Text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
Entity Framework - All Text Articles
Entity Framework - All Slides
Entity Framework Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
So far in this video series, we have been manually populating the database with test data using the insert sql script. Entity Framework can automate this. We will be working with the example we worked with in Part 5. Here are the steps.
using System.Collections.Generic;
using System.Data.Entity;
namespace Demo
{
public class EmployeeDBContextSeeder : DropCreateDatabaseIfModelChanges[EmployeeDBContext]
{
protected override void Seed(EmployeeDBContext context)
{
Department department1 = new Department()
{
Name = "IT",
Location = "New York",
Employees = new List[Employee]()
{
new Employee()
{
FirstName = "Mark",
LastName = "Hastings",
Gender = "Male",
Salary = 60000,
JobTitle = "Developer"
},
new Employee()
{
FirstName = "Ben",
LastName = "Hoskins",
Gender = "Male",
Salary = 70000,
JobTitle = "Sr. Developer"
},
}
};
Department department2 = new Department()
{
Name = "HR",
Location = "London",
Employees = new List[Employee]()
{
new Employee()
{
FirstName = "Philip",
LastName = "Hastings",
Gender = "Male",
Salary = 45000,
JobTitle = "Recruiter"
},
}
};
Department department3 = new Department()
{
Name = "Payroll",
Location = "Sydney",
Employees = new List[Employee]()
{
new Employee()
{
FirstName = "Steve",
LastName = "Pound",
Gender = "Male",
Salary = 45000,
JobTitle = "Sr. Payroll Admin",
},
}
};
context.Departments.Add(department1);
context.Departments.Add(department2);
context.Departments.Add(department3);
base.Seed(context);
}
}
}
Database.SetInitializer(new EmployeeDBContextSeeder());
[Table("tblEmployees")]
[Column("First_Name")]
At this point Employee class should look as shown below.
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public int DepartmentId { get; set; }
[ForeignKey("DepartmentId")]
public Department Department { get; set; }
public string JobTitle { get; set; }
}
Step 5: Run the application and notice that the Sample database, Departments and Employees tables are created and populated with test data automatically.
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
Entity Framework - All Text Articles
Entity Framework - All Slides
Entity Framework Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
So far in this video series, we have been manually populating the database with test data using the insert sql script. Entity Framework can automate this. We will be working with the example we worked with in Part 5. Here are the steps.
using System.Collections.Generic;
using System.Data.Entity;
namespace Demo
{
public class EmployeeDBContextSeeder : DropCreateDatabaseIfModelChanges[EmployeeDBContext]
{
protected override void Seed(EmployeeDBContext context)
{
Department department1 = new Department()
{
Name = "IT",
Location = "New York",
Employees = new List[Employee]()
{
new Employee()
{
FirstName = "Mark",
LastName = "Hastings",
Gender = "Male",
Salary = 60000,
JobTitle = "Developer"
},
new Employee()
{
FirstName = "Ben",
LastName = "Hoskins",
Gender = "Male",
Salary = 70000,
JobTitle = "Sr. Developer"
},
}
};
Department department2 = new Department()
{
Name = "HR",
Location = "London",
Employees = new List[Employee]()
{
new Employee()
{
FirstName = "Philip",
LastName = "Hastings",
Gender = "Male",
Salary = 45000,
JobTitle = "Recruiter"
},
}
};
Department department3 = new Department()
{
Name = "Payroll",
Location = "Sydney",
Employees = new List[Employee]()
{
new Employee()
{
FirstName = "Steve",
LastName = "Pound",
Gender = "Male",
Salary = 45000,
JobTitle = "Sr. Payroll Admin",
},
}
};
context.Departments.Add(department1);
context.Departments.Add(department2);
context.Departments.Add(department3);
base.Seed(context);
}
}
}
Database.SetInitializer(new EmployeeDBContextSeeder());
[Table("tblEmployees")]
[Column("First_Name")]
At this point Employee class should look as shown below.
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public int DepartmentId { get; set; }
[ForeignKey("DepartmentId")]
public Department Department { get; set; }
public string JobTitle { get; set; }
}
Step 5: Run the application and notice that the Sample database, Departments and Employees tables are created and populated with test data automatically.
Комментарии