Monday, November 12, 2018

পার্ট 2 - Entity Framework Model First Approach

এই আর্টিকেলে আমরা Entity Framework এর Model First Approach নিয়ে আলোচনা করবো। এই আর্টিকেলটি পার্ট 1 এর continuation, তাই এই আর্টিকেলটি proceed করার আগে পার্ট 1 এ একবার চোখ বুলিয়ে আসুন বুঝতে সুবিধা হবে।

Entity Framework ৩ ধরনের approaches support করেঃ
1. Schema First Approach - আগের আর্টিকেলে discuss করেছি
2. Model First Approach - এই আর্টিকেলে discuss করবো
3. Code First Approach - পরবর্তী আর্টিকেলে discuss করবো


Model First Approach এ আমরা প্রথমে Entity Model তৈরি করবো, তারপরে
1. Entities
2. Relationships between entities
3. Inheritance hierarchies etc.

আমরা এই কাজগুলো সরাসরি EDMX file এর design surface এ করবো। আমরা পার্ট-১ এ যে Demo project টা তৈরি করেছিলাম ঐটাতেই continue করবো।

N.B: Model First Approach এর practical ধাপগুলো আমি আপনাদের বোঝার সুবিধার জন্য English এ explain করবো। এই step গুলো VS2017(Visual Studio 2017) এ execute করা হয়েছে।


Step 1: First delete the "EmployeeModel.edmx" file from the existing solution

Step 2: Add a new "ADO.NET Entity Data Model" file with name = EmployeeModel.

Step 3: On "Choose Model Contents" screen, select "Empty EF Designer model" and click "Finish"
Model-first example in entity framework bangla
Model-first example in entity framework

Step 4:
At this point, we have a blank EmployeeModel.edmx file added to the solution. Now, right click on the designer surface and then select: Add - Entity. Set,
Entity Name = Department
Base Type = None
Entity Set = Departments
Create Key property = Select the checkbox
Property Name = Id
Property Type = Int32

Finally, click OK
Creating entity with model first approach
Creating an entity with the model first approach

Step 5:
Right-click on "Department" entity and select "Add - Scalar Property". Change the property name to "Name". Right click on the "Name" property that we just added and select "properties" from the context menu. Notice that the "Type" of "Name" property is set to "String".

Step 6: Similarly add "Location" property for the "Department" entity. At this point, the "Department" entity should look as shown below.
Department Entity
Department Entity

Step 7:
Add "Employee" entity and the following scalar properties.
FirstName (Type = string)
LastName (Type = string)
Gender (Type = string)
Salary (Type = int)

Step 8: At this point, Department and Employee entities should look as shown below.
Department and Employee Entities
Department and Employee Entities


Step 9: We need to add "Employees" navigation property for the "Department" entity, and "Department" navigation property for the "Employee" entity. To do this, right click on the design surface and select "Add - Association". In the "Add Association" window, set values as shown in the image below and click "OK"
How to Add Navigation Property
How to Add Navigation Property

Step 10:
At this point, we should have the following created.
a) Employees navigation property in the Department entity
b) Department navigation property in the Employee entity
c) DepartmentId scalar property
Employee and Department entity with navigation property
Employee and Department entity with the navigation property

Step 11: Right-click on the design surface, and select "Generate Database from Model..." option

Step 12: On "Choose Your Data Connection" screen, click "Next"

Step 13: At this point, we should have the required SQL generated to create
a) Departments table
b) Employees table
c) Primary key constraints for Departments and Employees tables
d) Foreign key constraint
e) Indexes

Step 14: Click Finish. Now, we should have "EmployeeModel.edmx.sql" file generated with the required SQL. If you already have "Departments" and "Employees" tables in the database, delete them. select "Execute SQL" option from the context menu, and execute it.

Step 15: Populate the "Departments" and "Employees" tables with sample data using the script below.
Insert into Departments values ('IT', 'New York')
Insert into Departments values ('HR', 'London')
Insert into Departments values ('Payroll', 'Sydney')

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

Step 16: Run the application by pressing CTRL + F5. We will get the following runtime error.

The specified default EntityContainer name 'EmployeeDBContext' could not be found in the mapping and metadata information.
Parameter name: defaultContainerName

Step 17: Open WebForm1.aspx in source mode, and set ConnectionString and DefaultContainerName properties of EntityDataSource control as shown below.

<asp:EntityDataSource ID="EntityDataSource1" runat="server"
    ConnectionString="name=EmployeeModelContainer"
    DefaultContainerName="EmployeeModelContainer"
    EnableFlattening="False"
    EntitySetName="Departments" Include="Employees">
</asp:EntityDataSource>

At this point, we should get the output like following.



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


No comments:

Post a Comment