Reader small image

You're reading from  Entity Framework Tutorial (Update) - Second Edition

Product typeBook
Published inAug 2015
Reading LevelIntermediate
Publisher
ISBN-139781783550012
Edition2nd Edition
Languages
Right arrow
Author (1)
Joydip Kanjilal
Joydip Kanjilal
author image
Joydip Kanjilal

Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP (Most Valuable Professional) award at ASP.Net for 2007, 2008, 2009, 2010, 2011, and 2012. He is currently working as a Chief Software Architect at a reputed software company in Hyderabad. Joydip has almost 20 years of industry experience in IT, with more than 14 years in Microsoft .NET and its related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times. He is the author of several books and more than 250 articles. Many of his articles have been featured at Microsoft's Official Site on ASP.NET. Joydip has authored the following books:- ASP.NET Web API (Packt Publishing) Visual Studio Six in One (Wrox Publishers) ASP.NET 4.0 Programming (Mc-Graw Hill Publishing) Entity Framework Tutorial (Packt Publishing) Pro Sync Framework (APRESS) Sams Teach Yourself ASP.NET Ajax in 24 Hours (Sams Publishing) ASP.NET Data Presentation Controls Essentials (Packt Publishing) Joydip has also reviewed more than a dozen books till date. He was a speaker a speaker at the reputed Spark IT 2010 event and at the reputed Dr. Dobb's Conference 2014 in Bangalore. He's also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He blogs these days at: http://www.infoworld.com/blog/microsoft-coder
Read more about Joydip Kanjilal

Right arrow

Chapter 2. Getting Started

In the previous chapter, we took a look at Entity Framework, including its architecture and its features. We also had a look at the new and enhanced features in Entity Framework 7. Note that Entity Framework 6.0 ships with Visual Studio 2013, but you can also install it via NuGet if needed. Also, Entity Framework 7 is yet to be released.

In this chapter, we will design our Security database, create an Entity Data Model (EDM) on top of it, and then use the EntityDataSource control to bind data exposed by the EDM to a GridView control.

The DataSource controls are those that are used to connect to a data source and then retrieve data from those data sources. If you use DataSource controls, the need of writing tedious code to perform Create, Read, Update, Delete (CRUD) operations on data-based controls is eliminated. The EntityDataSource control is a DataSource control that can connect to the data exposed by the EDM to perform CRUD operations.

In this chapter, we will...

Designing the UserAuthentication database


Before we begin implementing a simple application that shows how data retrieved from the EDM can be consumed, let's take a quick look at the EDM again:

As you can see in this diagram, our application needs to interact with the CSDL layer. The SSDL layer will connect to the database (the Security database in our example), and the mapping layer will map these two layers so that they can communicate.

The Security database comprises of a list of the following tables:

  • Users: This table contains the user details.

  • UsersAuthentication: This table contains the user authentication details.

  • UserAuthenticationTypes: This table contains user authentication type data. User authentication type can be Windows, Forms, Passport, and Anonymous.

  • UsersLoginHistory: This table contains data related to user login history; that is, the user login history data of the user.

  • UserRoles: This table contains the user role details.

  • Roles: This table contains the role details.

  • Controls...

Creating the EDM


Now that the Security database is ready, we will explore how we can create an EDM on top of the Security database.

Note

Note that, before Entity Framework 7, there were two storage models—the EDMX file format based on XML schema or code. With Entity Framework 7, the EDMX file format will be dropped—we will have only the code-based format. Interestingly, this approach is also termed the "code-first only" approach.

You can create the 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

The first approach is preferred to the second. However, as we move through the chapters of the book, we will explore how we can follow the code-first approach to implement the model for our application that uses Entity Framework.

We will first take a look at how we can design an EDM using the ADO.NET Entity Data Model Designer.

Creating the Entity Data Model using the ADO.NET Entity Data Model Designer

To create...

The DataSource controls


The DataSource controls are those that can be bound to data from external data sources. These data sources may include databases, XML files, or even flat files. ASP.NET 2.0 introduced some DataSource controls with a powerful data binding technique, so the need for writing lengthy code for binding data-to-data controls has been eliminated.

Note

In ASP.NET, the term data binding implies binding the controls to data retrieved from a data source and providing read or write connectivity between these controls and the data that they are bound to.

A DataSource control acts like a layer in between your data source and the data bound control. Data bound controls can be any control that actually interacts with the end user while consuming the data services provided by the DataSource control to which it is bound. It defines certain methods and properties that perform data-specific operations like insert, delete, update, and select over the data exposed by the DataSource control...

Implementing our first application using the Entity Framework


In this section, we will learn how to use the EDM and the EntityDataSource control to implement our first program using the Entity Framework. We will use a GridView control to display bound data.

Let's first have the environment ready. I will run you through the steps to download and install Entity Framework 6 now.

In the Solution Explorer, right-click on the project and select EntityFramework from the list of NuGet packages, as shown in the following screenshot:

Click on Install to start downloading and installing Entity Framework 6:

Once Entity Framework 6 has been downloaded, click on Accept to start the installation. After Entity Framework 6 has been successfully installed and the necessary changes applied to your project, you are ready to start writing your first application that makes use of this framework.

Refer to the solution we created earlier using the Entity Data Model Designer. Now follow these steps:

  1. Drag and drop an EntityDataSource...

Summary


In this chapter, we discussed how we can get started with Entity Framework. You learned how to create an EDM and use it along with the EntityDataSource control, to bind data to a GridView data control. In this chapter, we created the UserAuthentication database and an EDM that exposed this database. We also explored the EntityDataSource control and used it to bind data in our first application that leverages Entity Framework 6.

In the next chapter, we will continue to explore the EDM including each of its sections and will learn how they are related to each other.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Entity Framework Tutorial (Update) - Second Edition
Published in: Aug 2015Publisher: ISBN-13: 9781783550012
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Joydip Kanjilal

Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP (Most Valuable Professional) award at ASP.Net for 2007, 2008, 2009, 2010, 2011, and 2012. He is currently working as a Chief Software Architect at a reputed software company in Hyderabad. Joydip has almost 20 years of industry experience in IT, with more than 14 years in Microsoft .NET and its related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times. He is the author of several books and more than 250 articles. Many of his articles have been featured at Microsoft's Official Site on ASP.NET. Joydip has authored the following books:- ASP.NET Web API (Packt Publishing) Visual Studio Six in One (Wrox Publishers) ASP.NET 4.0 Programming (Mc-Graw Hill Publishing) Entity Framework Tutorial (Packt Publishing) Pro Sync Framework (APRESS) Sams Teach Yourself ASP.NET Ajax in 24 Hours (Sams Publishing) ASP.NET Data Presentation Controls Essentials (Packt Publishing) Joydip has also reviewed more than a dozen books till date. He was a speaker a speaker at the reputed Spark IT 2010 event and at the reputed Dr. Dobb's Conference 2014 in Bangalore. He's also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He blogs these days at: http://www.infoworld.com/blog/microsoft-coder
Read more about Joydip Kanjilal