The ADO.NET Entity Framework, the next generation of Microsoft's data access technology, is an extended Object Relational Mapping (ORM) technology that makes it easy to tie together the data in your database with the objects in your applications. This is done by abstracting the object model of an application from its relational or logical model. It is an extended ORM in the sense that it provides many additional features over an ORM. Some of these features are:
Creating an Entity Data Model
You can create the ADO.NET Entity Data Model in one of two ways:
Creating the Payroll Entity Data Model Using the ADO.NET Entity Data Model Designer
Here again are the tables of our Payroll database that we will use to generate the data model:
1. Open Visual Studio.NET and create a solution for a new web application project as seen below and save with a name.
2. Switch to the Solution Explorer, right click and click on Add New Item as seen in the following screenshot:
3. Next, select ADO.NET Entity Data Model from the list of the templates displayed as shown in the following screenshot:
4. Name the Entity Data Model PayrollModel and click on Add.
5. Select Generate from database from the Entity Data Model Wizard as shown in the following screenshot:
Note that you can also use the Empty model template to create the Entity Data Model yourself.
If you select the Empty Data Model template and click on next, the following screen appears:
As you can see from the above figure, you can use this template to create the Entity Data Model yourself. You can create the Entity Types and their relationships manually by dragging items from the toolbox. We will not use this template in our discussion here. So, let's get to the next step.
6. Click on Next in the Entity Data Model Wizard window shown earlier.
7. The modal dialog box will now appear and prompts you to choose your connection as shown in the following figure:
8. Click on New Connection Now you will need to specify the connection properties and parameters as shown in the following figure:
We will use a dot to specify the database server name. This implies that we will be using the database server of the localhost, which is the current system in use.
9. After you specify the necessary user name, password, and the server name, you can test your connection using the Test Connection button. When you do so, the message Test connection succeeded gets displayed in the message box as shown in the previous figure.
10. When you click on OK on the Test connection dialog box, the following screen appears:
Note the Entity Connection String generated automatically. This connection string will be saved in the ConnectionStrings section of your application's web.config file. This is how it will look like:
<connectionStrings>
<add name="PayrollEntities" connectionString="metadata=res://
*;provider=System.Data.SqlClient;provider connection
string="Data Source=.;Initial Catalog=Payroll;User
ID=sa;Password=joydip1@3;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
11. When you click on Next in the previous figure, the following screen appears:
12. Expand the Tables node and specify the database objects that you require in the Entity Data Model to be generated as shown in the following figure:
13. Click on Finish to generate the Entity Data Model.
Your Entity Data Model has been generated and saved in a file named PayrollModel.edmx. We are done creating our first Entity Data Model using the ADO.NET Entity Data Model Designer tool.
Note how the Entity Types in the above model are related to one another. These relationships have been generated automatically by the Entity Data Model Designer based on the relationships between the tables of the Payroll database we created in the previous chapter.
we will now take a look at how to create a data model using the Entity Data Model generation tool called EdmGen. The EdmGen.exe command line tool can be used to do one or more of the following:
The EdmGen.exe command line tool generates the Entity Data Model as a set of three files: .csdl, .msl, and .ssdl. If you have used the ADO.NET Entity Data Model Designer to generate your Entity Data Model, the .edmx file generated will contain the CSDL, MSL, and the SSDL sections. You will have a single .edmx file that bundles all of these sections into it. On the other hand, if you use the EdmGen.exe tool to generate the Entity Data Model, you would find three distinctly separate files with .csdl, .msl or .ssdl extensions.
Here is a list of the major options of the EdmGen.exe command line tool:
Note that you basically need to pass the connection string, specify the mode, and also the project name of the artifact files (.csdl, .msl, and the .ssdl files) to be created. To create the Entity Data Model for our database, open a command window and type in the following:
edmgen /mode:fullgeneration /c:"Data Source=.;Initial Catalog=Payroll;User ID=sa;Password=joydip1@3;" /p:Payroll
This will create a full ADO.NET Entity Data Model for our database. The output is shown in the following figure:
You can now see the list of the files that have been generated:
You can validate the Payroll Entity Data Model that was just created, using the ValidateArtifacts option of the EdmGen command line tool as shown below:
EdmGen /mode:ValidateArtifacts /inssdl:Payroll.ssdl /inmsl:Payroll.msl /incsdl:Payroll.csdl
When you execute the above command, the output will be similar to what is shown in the following figure:
As you can see in the previous figure, there are no warnings or errors displayed. So, our Entity Data Model is perfect.
The section that follows discusses the new Entity Data Source control which was introduced as part of the Visual Studio.NET 2008 SP1 release.
This article is part three of a four part series on the ADO.NET Entity Framework. This article discusses the new Entity Data Source control which was introduced as part of the Visual Studio.NET 2008 SP1 release.
Data controls are those that can be bound to data from external data sources. These data sources may include databases, XML files or flat files. ASP.NET 2.0 introduced some data source controls with a powerful data binding technique that eliminated the need to write lengthy code for binding data to data controls.
In ASP.NET the term Data Binding implies binding the controls to data retrieved from a data source and providing a read or write connectivity between these controls and the data that they are bound to.
The Entity Data Source control is an example of a data control that is included as part of the Visual Studio 2008 SP1 release and can be used to bind data retrieved from an Entity Data Model to the data bound controls of ASP.NET. If Visual Studio 2008 SP1 is installed, the EntityDataSource control can be seen listed in the Data section of the toolbox.
If the EntityDataSource control is not in the toolbox, follow these steps:
1. Right-click on the Toolbox and select the Choose Items option as shown in the following figure:
2. From the list of the components displayed, scroll down to locate the EntityDataSource in the .NET Framework Components tab. This is the first component selected in the below figure:
3. Select the checkbox next to the EntityDataSource component and click on OK. The ADO.NET Entity Data Source control is now added to the toolbox as shown in the following figure:
If the EntityDataSource component is not listed in the list of the components displayed in the Choose Toolbox Items window, it has to be added manually. To do this, click on the Browse button in the Choose Toolbox Items window, locate the System.Web.Entity.dll in the folder where Microsoft .NET Framework 3.5 has been installed and click OK.
This article is the final article of a 4-part series on the ADO.NET Entity Framework. This article will describe how to use the Entity Data Model and the Entity Data Source Control to implement a program using the Entity Framework. Using a GridView control to display bound data. Refer to the solution we created earlier using the Entity Data Model Designer and follow the steps below:
1. Drag and drop an Entity Data Source control from the toolbox onto your Default.aspx web form.
2. Click on the Configure Data Source option to specify the data source as shown below.
3. Specify the Connection String and DefaultContainerName and then click on Next.
4. Specify the fields you would want to retrieve from the database table and when done click Finish.
5. Drag and drop a GridView control from the toolbox onto the Default.aspx web form as seen below.
6. Use the Choose Data Source option of the GridView control to associate its data source with the Entity Data Source control we created earlier. Refer to the following figure:
Below is how the markup code of the GridView control looks with its templates defined. Note how the DataSourceID of the GridView control has been associated with the Entity Data Source control created earlier.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=
"False" DataKeyNames="EmployeeID"
DataSourceID="SqlDataSource1" BorderColor="Black" BorderStyle="Solid" Width="400px">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText=
"First Name" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText=
"Last Name" SortExpression="LastName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
</Columns>
</asp:GridView>
It's now done! When the application is executed, the output should be similar to what is shown below:
Summary
This series of articles has discussed how to get started with the ADO.NET Entity Framework. Learning how to create an Entity Data Model and use it along with the Entity Data Source control to bind data to a GridView data control.
- Entity Inheritance and Composition
- Identity Resolution and Change Tracking
- LINQ Support
- The Object Service Layer
- Creating an Entity Data Model
- Introducing the Entity Data Source Control
- Implementing our first application using the ADO.NET Entity Framework
Creating an Entity Data Model
You can create the ADO.NET Entity Data Model in one of two ways:
- Use the ADO.NET Entity Data Model Designer
- Use the command line Entity Data Model Designer called EdmGen.exe
Creating the Payroll Entity Data Model Using the ADO.NET Entity Data Model Designer
Here again are the tables of our Payroll database that we will use to generate the data model:
- Employee
- Designation
- Department
- Salary
- Provident Fund
1. Open Visual Studio.NET and create a solution for a new web application project as seen below and save with a name.
2. Switch to the Solution Explorer, right click and click on Add New Item as seen in the following screenshot:
3. Next, select ADO.NET Entity Data Model from the list of the templates displayed as shown in the following screenshot:
4. Name the Entity Data Model PayrollModel and click on Add.
5. Select Generate from database from the Entity Data Model Wizard as shown in the following screenshot:
Note that you can also use the Empty model template to create the Entity Data Model yourself.
If you select the Empty Data Model template and click on next, the following screen appears:
As you can see from the above figure, you can use this template to create the Entity Data Model yourself. You can create the Entity Types and their relationships manually by dragging items from the toolbox. We will not use this template in our discussion here. So, let's get to the next step.
6. Click on Next in the Entity Data Model Wizard window shown earlier.
7. The modal dialog box will now appear and prompts you to choose your connection as shown in the following figure:
8. Click on New Connection Now you will need to specify the connection properties and parameters as shown in the following figure:
We will use a dot to specify the database server name. This implies that we will be using the database server of the localhost, which is the current system in use.
9. After you specify the necessary user name, password, and the server name, you can test your connection using the Test Connection button. When you do so, the message Test connection succeeded gets displayed in the message box as shown in the previous figure.
10. When you click on OK on the Test connection dialog box, the following screen appears:
Note the Entity Connection String generated automatically. This connection string will be saved in the ConnectionStrings section of your application's web.config file. This is how it will look like:
<connectionStrings>
<add name="PayrollEntities" connectionString="metadata=res://
*;provider=System.Data.SqlClient;provider connection
string="Data Source=.;Initial Catalog=Payroll;User
ID=sa;Password=joydip1@3;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
11. When you click on Next in the previous figure, the following screen appears:
12. Expand the Tables node and specify the database objects that you require in the Entity Data Model to be generated as shown in the following figure:
13. Click on Finish to generate the Entity Data Model.
Your Entity Data Model has been generated and saved in a file named PayrollModel.edmx. We are done creating our first Entity Data Model using the ADO.NET Entity Data Model Designer tool.
Note how the Entity Types in the above model are related to one another. These relationships have been generated automatically by the Entity Data Model Designer based on the relationships between the tables of the Payroll database we created in the previous chapter.
we will now take a look at how to create a data model using the Entity Data Model generation tool called EdmGen. The EdmGen.exe command line tool can be used to do one or more of the following:
- Generate the .cdsl, .msl, and .ssdl files as part of the Entity Data Model
- Generate object classes from a .csdl file
- Validate an Entity Data Model
The EdmGen.exe command line tool generates the Entity Data Model as a set of three files: .csdl, .msl, and .ssdl. If you have used the ADO.NET Entity Data Model Designer to generate your Entity Data Model, the .edmx file generated will contain the CSDL, MSL, and the SSDL sections. You will have a single .edmx file that bundles all of these sections into it. On the other hand, if you use the EdmGen.exe tool to generate the Entity Data Model, you would find three distinctly separate files with .csdl, .msl or .ssdl extensions.
Here is a list of the major options of the EdmGen.exe command line tool:
Note that you basically need to pass the connection string, specify the mode, and also the project name of the artifact files (.csdl, .msl, and the .ssdl files) to be created. To create the Entity Data Model for our database, open a command window and type in the following:
edmgen /mode:fullgeneration /c:"Data Source=.;Initial Catalog=Payroll;User ID=sa;Password=joydip1@3;" /p:Payroll
This will create a full ADO.NET Entity Data Model for our database. The output is shown in the following figure:
You can now see the list of the files that have been generated:
You can validate the Payroll Entity Data Model that was just created, using the ValidateArtifacts option of the EdmGen command line tool as shown below:
EdmGen /mode:ValidateArtifacts /inssdl:Payroll.ssdl /inmsl:Payroll.msl /incsdl:Payroll.csdl
When you execute the above command, the output will be similar to what is shown in the following figure:
As you can see in the previous figure, there are no warnings or errors displayed. So, our Entity Data Model is perfect.
The section that follows discusses the new Entity Data Source control which was introduced as part of the Visual Studio.NET 2008 SP1 release.
This article is part three of a four part series on the ADO.NET Entity Framework. This article discusses the new Entity Data Source control which was introduced as part of the Visual Studio.NET 2008 SP1 release.
Data controls are those that can be bound to data from external data sources. These data sources may include databases, XML files or flat files. ASP.NET 2.0 introduced some data source controls with a powerful data binding technique that eliminated the need to write lengthy code for binding data to data controls.
In ASP.NET the term Data Binding implies binding the controls to data retrieved from a data source and providing a read or write connectivity between these controls and the data that they are bound to.
The Entity Data Source control is an example of a data control that is included as part of the Visual Studio 2008 SP1 release and can be used to bind data retrieved from an Entity Data Model to the data bound controls of ASP.NET. If Visual Studio 2008 SP1 is installed, the EntityDataSource control can be seen listed in the Data section of the toolbox.
If the EntityDataSource control is not in the toolbox, follow these steps:
1. Right-click on the Toolbox and select the Choose Items option as shown in the following figure:
2. From the list of the components displayed, scroll down to locate the EntityDataSource in the .NET Framework Components tab. This is the first component selected in the below figure:
3. Select the checkbox next to the EntityDataSource component and click on OK. The ADO.NET Entity Data Source control is now added to the toolbox as shown in the following figure:
If the EntityDataSource component is not listed in the list of the components displayed in the Choose Toolbox Items window, it has to be added manually. To do this, click on the Browse button in the Choose Toolbox Items window, locate the System.Web.Entity.dll in the folder where Microsoft .NET Framework 3.5 has been installed and click OK.
This article is the final article of a 4-part series on the ADO.NET Entity Framework. This article will describe how to use the Entity Data Model and the Entity Data Source Control to implement a program using the Entity Framework. Using a GridView control to display bound data. Refer to the solution we created earlier using the Entity Data Model Designer and follow the steps below:
1. Drag and drop an Entity Data Source control from the toolbox onto your Default.aspx web form.
2. Click on the Configure Data Source option to specify the data source as shown below.
3. Specify the Connection String and DefaultContainerName and then click on Next.
4. Specify the fields you would want to retrieve from the database table and when done click Finish.
5. Drag and drop a GridView control from the toolbox onto the Default.aspx web form as seen below.
6. Use the Choose Data Source option of the GridView control to associate its data source with the Entity Data Source control we created earlier. Refer to the following figure:
Below is how the markup code of the GridView control looks with its templates defined. Note how the DataSourceID of the GridView control has been associated with the Entity Data Source control created earlier.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=
"False" DataKeyNames="EmployeeID"
DataSourceID="SqlDataSource1" BorderColor="Black" BorderStyle="Solid" Width="400px">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText=
"First Name" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText=
"Last Name" SortExpression="LastName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
</Columns>
</asp:GridView>
It's now done! When the application is executed, the output should be similar to what is shown below:
Summary
This series of articles has discussed how to get started with the ADO.NET Entity Framework. Learning how to create an Entity Data Model and use it along with the Entity Data Source control to bind data to a GridView data control.
No comments:
Post a Comment