Quantcast
Viewing all articles
Browse latest Browse all 21

ASP.NET EntityDataSource and EntityFramework: A Beginners Guide

What is an EntityDataSource?

An entity data source is a web control designed to interact with the data contained in an EntityFramework (EF). Firstly we will examine EFs, then move on to how EntityDataSources interact with the EFs.

What is an EntityFramework (EF)?

An EF is a construct used in .NET to represent the data contained in a relational database, in code form. These types of constructs are known as Object-Relational Mappers (ORM). Put simply, these objects will represent each table in a database as a type of object, and each record in a table as an object of this type. For instance if a table looks like this:

Image may be NSFW.
Clik here to view.
artistTable

  • The name of the table is Artists, so the type for this table in an EF would be Artist.
  • Each Artist object would have properties of ID, Name and RecordLabel.

If we have another table with a foreign key the references this table, that looks like this:

Image may be NSFW.
Clik here to view.
albumsTable

  • The name of the table is Albums, so the type for this table would be Album.
  • Each Album would have the properties of ID, Title, Description and TrackCount.
  • Each Album would also have an Artist object related to it, via the foreign key.
  • Conversely, each Artist can now have a number of Albums related to it (0…*).

Creating the EntityFramework

Suppossing we have created the database shown above, visual studio will allow us to generate an EF from the database. This is known as database first EF.

The first step is to add a new EF to your project. Right click the project in the project explorer, and select add new item, then ADO.NET Entity Data Model:

Image may be NSFW.
Clik here to view.
addEF

 

Give your model a name, and then add it. The wizard will then ask if you want to create and empty model or generate the model from a database, and in this case we are going to generate the model from a database. The next dialogue will ask you for the database details, and generate a connection string. In the “Save entity connection settings in web.config” area, give the settings a suitable name before moving on. The next dialogue will ask you which database objects you wish to include in the model – we are just including the tables as shown below, then selecting the finish option:

Image may be NSFW.
Clik here to view.
efSetup1

Once visual studio has finished generating the code you will be displayed with the .edmx file for your model, and a diagram representing the tables and their relationships. This signals that the EF has been created and can now be used within the project. For instance:

//Create new EF database object
            MusicLibraryContext db = new MusicLibraryContext();

            //assign the first record in the Albums table to a variable
            var album = db.Albums.FirstOrDefault();

            //delete the selected album
            db.Albums.DeleteObject(album);

            //save changes to the database
            db.SaveChanges();

You will notice that intellisense will show that the album object has all of the properties of the records in the Albums table:

Image may be NSFW.
Clik here to view.
efSetup2

 

Using EntityDataSources

An EntityDataSource is a control that can be used to quickly translate EF data access functions to a web page – allowing the usual CRUD functionality. To add a EntityDataSource to your page, find it under the Data section in the toolbox, and drag it into the code or design view in your .aspx file (Note: the project may need to build before these steps). The generated code will look something like this, after configuration in the design view as shown:

Image may be NSFW.
Clik here to view.
configEDS
 Image may be NSFW.
Clik here to view.
configEDS2
 Image may be NSFW.
Clik here to view.
configEDS3

<%--Added the Include so that the data source includes the Artist data--%>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
            ConnectionString="name=MusicDBContext" 
            DefaultContainerName="MusicDBContext" 
            EnableDelete="True" EnableFlattening="False" EnableInsert="True" 
            EnableUpdate="True" EntitySetName="Albums" 
            EntityTypeFilter="Album" Include="Artist">

</asp:EntityDataSource>

Note that the Include property will ensure that the relevant data in the Artist object/record is included.

Our EntityDataSource is now present, and we can begin using it to provide functionality to databound controls. From the toolbox drag a new listview into your page, and select the EntityDataSource as the data source:

Image may be NSFW.
Clik here to view.
configEDS4

<asp:ListView ID="ListView2" runat="server" DataSourceID="EntityDataSource1">
</asp:ListView>

From here, configure your control using the same technique. Notice that the options for editing, inserting, deleting and paging are present. Once this step is complete, visual studio will generate markup for the various views available to the control.

The completed page

At this point, everything should be functional – hit f5 and test! Our example looks like this:

Image may be NSFW.
Clik here to view.
example

Note that the default display in the Album column will NOT be the name of the album, but the Album itself (.ToString()). We need to point the ListView to the Album.Name in the appropriate templates. For example:

<ItemTemplate>
                <tr style="">

                    ...

                    <%--Added Artist.Name to the Eval--%>
                    <td>
                        <asp:Label ID="ArtistLabel" runat="server" 
                             Text='<%# Eval("Artist.Name") %>' />
                    </td>
                </tr>
</ItemTemplate>

This tutorial has only started to scratch the surface as far as EntityFramework and EntityDataSources are concerned – yet we have quickly generated code that allows a web application to use the data access functions that come with EntityFramework.

The post ASP.NET EntityDataSource and EntityFramework: A Beginners Guide appeared first on Weebtutorials.


Viewing all articles
Browse latest Browse all 21

Trending Articles