Often developers need to understand the database before they can write ac¬cess codes for it. Understanding complex database structure that is controlled by administrator becomes a bottleneck in ap¬plication development cycle. Using Microsoft's ADO. NET Entity Framework, developers can . create an entity relationship model of the data¬base (ie SQL Server) inside Visual Studio 2008. This conceptual model is then used by devel¬opers to write code against database and that in turn reduces code development time along with lines of code. This is a step by Microsoft to increase abstraction, making things efficient for developers.

Consider this example: when we write code to access data from a database (SQL Server), we often have to write queries containing'Joins: which can only be used if there is complete unerstanding of the underlying data base structure. Another issue is regarding writting connection strings' to connect and disconnect from database. Here is a sample code that shows how developers normally write the code:

In this code, developer is spending too much time on database related issues. The
developing application ADO.NET Entity Framework along with LINQ tries to address these issues making application development simple and quick.

Implementing ADO.NET' Entity Framework

In this section we will show how to use ADO, NET entity framework feature for existing database and how it simplifies the above code. Before you actually start using this feature, you should have Visual Studio 2008 SPI installed on your system. We will start with creating a 'Windows Form Application' project in VS by the name of'PCQLADOEF.' We shall use C# as the programming language. Once the project has been created, we will add an 'ADO.NET Entity Data Model' item to the project. This can be done by clicking on 'Project' and then on 'Add New Item.' This action will start a wizard; from this wizard select the appropriate database to create the entity model. Here we used 'School' database.

Name:  Querying SQL Server Becomes Simple.gif
Views: 30
Size:  13.7 KB

Now once the wizard finishes, you shall have the 'School.edmx' file created. Double clicking on this file will show the model in the Designer window. This is very helpful in understanding the structure of database in question. To understand the EDMX file more, right click on 'School.edmx' and open it with 'XML Editor'. There are three major portions of this file: Conceptual Model, Storage Model and Mappings. EntityType represents the table along with properties defining different fields and Association represents relationship of entity. Storage Model is the representation of database schema and Mappings defines movement between conceptual layer and actual storage. One can easily push back the changes made to conceptual layer to actual database by writing a single line of code.

The next step is to query the conceptual model or EDM (entity data model) model that we have just created. This can be done by adding the following controls to our form: DataGridView and ComboBox, and two buttons. Here DataGridView and ComboBox are for displaying data from EDM. One button is to update changes made to EDM back to actual database and the other is used to close the application. Here's the sample code:

Here's the code for 'button 1.' It closes the form and disconnects connection to theEDM:

To populate data from EDM todata grid of the form, the following code is written:

Finally the changes made to EDM need to be translated back to actual database, this is done by writing the following code on click event of button2?

With ADO.NET Entity Framework programming is simplified with the introduction of a new layer between actual database and application. The actual database here can only be SQL Server and not any other database; this can be seen as limiting factor though there are many open source APIs available for other databases.