Thursday, November 15, 2018

পার্ট 5 - How to handle model changes in entity framework

Suggested Articles
পার্ট 2 - Entity Framework Model First Approach
পার্ট 3 - Entity Framework Code First Approach
পার্ট 4 - Customizing table & column names

এই আর্টিকেলে আমরা discuss করবো, কিভাবে database created থাকা অবস্থায় model changes handle করতে হয়। এই আর্টিকেলটি পার্ট 4 এর continuation, তাই এই আর্টিকেলটি শুরু কারার আগে পার্ট 4 এ একবার চোখবুলিয়ে আসুন।

এখন Employee class টা এরকম আছে -

[Table("tblEmployees")]
public class Employee
{
    public int Id { getset; }
    [Column("First_Name")]
    public string FirstName { getset; }
    public string LastName { getset; }
    public string Gender { getset; }
    public int Salary { getset; }
    public int DepartmentId { getset; }
    [ForeignKey("DepartmentId")]
    public Department Department { getset; }
}

উপরের Employee class এর উপর basis করে Entity Framework নিচের মতো table তৈরি করেছিলো
The model backing the DBContext context has changed since the database was created. Consider using Code First Migrations to update the database
এখন আমরা Employee class এ JobTitle নামে একটা নতুন property add করতে চাই, তাহলে modified Employee class টা এরকম হবে -

[Table("tblEmployees")]
public class Employee
{
    public int Id { getset; }
    [Column("First_Name")]
    public string FirstName { getset; }
    public string LastName { getset; }
    public string Gender { getset; }
    public int Salary { getset; }
    public int DepartmentId { getset; }
    [ForeignKey("DepartmentId")]
    public Department Department { getset; }
    public string JobTitle { getset; }
}
এখন program টা run করলে নিচের মতো একটা error পাবো ( আগের আর্টিকেলে বলেছিলাম )

The model backing the 'EmployeeDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).


এটার কারন হল model (i.e Employee class) টা পরিবর্তিত হয়ে গেছে, যেখানে database টা আগেই তৈরি করা আছে। সহজ কথায় Model এবং database আর sync অবস্থাতে নেই, যার কারনে আমারা এই error টা পাচ্ছি। এই database created থাকা অবস্থাতে model পরিবর্তিত হলে entity framework __MigrationHistory table uses করে, যেটা auto-generated।

এই error fix করার জন্য আমরা entity framework কে বলে দেবো যে, model change হলে কি করতে হবে।

web application project এ Global.asax file টি Add করুন। সেই file এর Application_Start() method এ নিচের code টুকু copy করে paste করে দীন। এখানে আমরা entity framework কে বলে দিচ্ছি যে, যখনই model change হবে তখনই database recreate করতে হবে।

Database.SetInitializer(
                       newDropCreateDatabaseIfModelChanges<EmployeeDBContext>());


আরেকটা option আছে, আমার বলে দেবো যে প্রতিবারই database drop করে recreate করতে হবে। সেক্ষেত্রে Application_Start() method এ নিচের মতো code লিখতে হবে -

Database.SetInitializer(new DropCreateDatabaseAlways<EmployeeDBContext>());


Please Note: Database class টা System.Data.Entity namespace এ অবস্থিত ।


এখন Application টা run করলে webform এ কোন data show করবে না, কারন database drop হয়ে recreate হয়েছে। tblEmployees table টাতে data insert করার জন্য নিচের SQL query টা run করুন -

Insert into Departments values ('IT', 'New York')
Insert into Departments values ('HR', 'London')
Insert into Departments values ('Payroll', 'Sydney')

Insert into tblEmployees values ('Mark', 'Hastings', 'Male', 60000, 1, 'Developer')
Insert into tblEmployees values ('Steve', 'Pound', 'Male', 45000, 3, 'Manager')
Insert into tblEmployees values ('Ben', 'Hoskins', 'Male', 70000, 1, 'Developer')
Insert into tblEmployees values ('Philip', 'Hastings', 'Male', 45000, 2, 'Recruiter')
Insert into tblEmployees values ('Mary', 'Lambeth', 'Female', 30000, 2, 'Recruiter')
Insert into tblEmployees values ('Valarie', 'Vikings', 'Female', 35000, 3, 'Manager')
Insert into tblEmployees values ('John', 'Stanmore', 'Male', 80000, 1, 'Developer')

webform refresh করুন, নিচের মতো output পাবেন -

Handle model changes in entity framework Output
Output


উপরের explanations আরও ভালভাবে বুঝতে এই video টি একবার দেখে আসুন। ধন্যবাদ।।



1 comment: