Part 3 - Entity Framework Code First Approach
Part 4 - Customizing table & column names
Part 5 - How to handle model changes in entity framework
এই আর্টিকেল series এ আমরা database কে সব সময়ই manually test data দিয়ে populating করেছি, মানে sql script দিয়ে database এ data insert এর কাজটা করেছি। Entity Framework এই data insert এর কাজটাকেউ automate করতে পারে। আমরা এই পর্বে পার্ট 5 এর example টা নিয়ে কাজ করবো। Here are the steps -
Step 1: project এর solution explorer এ Right click করে একটা class file add করুন,যার name হবে EmployeeDBContextSeeder.cs
Step 2: নিচের code টুকু Copy করে EmployeeDBContextSeeder.cs file এ paste করুন -
using System.Collections.Generic;
using System.Data.Entity;
namespace EntityFrameworkDemo
{
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"
},
new Employee()
{
FirstName = "John",
LastName = "Stanmore",
Gender = "Male",
Salary = 80000,
JobTitle = "Project Manager"
}
}
};
Department department2 = new Department()
{
Name = "HR",
Location = "London",
Employees = new List<Employee>()
{
new Employee()
{
FirstName = "Philip",
LastName = "Hastings",
Gender = "Male",
Salary = 45000,
JobTitle = "Recruiter"
},
new Employee()
{
FirstName = "Mary",
LastName = "Lambeth",
Gender = "Female",
Salary = 30000,
JobTitle = "Sr. 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",
},
new Employee()
{
FirstName = "Valarie",
LastName = "Vikings",
Gender = "Female",
Salary = 35000,
JobTitle = "Payroll Admin",
}
}
};
context.Departments.Add(department1);
context.Departments.Add(department2);
context.Departments.Add(department3);
base.Seed(context);
}
}
}
Step 3: Global.asax file এর Application_Start() method এ নিচের লাইনটা copy করে paste করে দীন
Database.SetInitializer(new EmployeeDBContextSeeder());
Step 4: Employee.cs file থেকে নিচের Table এবং Column attributes remove করে দীন -
[Table("tblEmployees")]
[Column("First_Name")]
এখন Employee class টা দেখতে নিচের মতো হওয়া উচিত
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: এখন application টা run করলে দেখবেন যে, Sample database, Departments এবং Employees tables created হয়েছে এবং test data দিয়ে automatically populated হয়েছে। webForm এ নিচের মতো output আসবে-
Output |
উপরের explanations আরও ভালভাবে বুঝতে এই video টি একবার দেখে আসুন। ধন্যবাদ।।
No comments:
Post a Comment