Introduction to the Microsoft Entity Framework

This document introduces BTI420 students to the Microsoft Entity Framework (EF). We will use EF for data persistence.

.

About the Entity Framework

Adapted from the MSDN Library documentation:

The Entity Framework is a set of technologies in ADO.NET that support the development of data-oriented software applications.

It enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored.

With the Entity Framework , developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code than in traditional applications.

.

Another way of describing EF:

Entity Framework is an object design, management, and persistence framework.

.

You should note that starting now, and continuing into the future, Microsoft and the industry recommend that developers use the Entity Framework in data-oriented applications.

For those who are familiar with ADO.NET, an Entity Framework data model is the next evolution of the ADO.NET data access layer (e.g. DataSet) technologies. (You are not permitted to use legacy and/or deprecated data access technologies in BTI420.)

.

Entity Framework data model

To use EF, we create a data model.

Right-click your App_Code folder, and Add New Item. Select the “ADO.NET Entity Data Model”, accept the default “Name”, and click the Add button.

This action will start the Entity Data Model Wizard.

At this point, you are asked to “Choose Model Contents“. You have two choices:

  • Generate the model from a database
  • Create a new model, by visually designing the entities yourself

.

At this point, you can click the wizard’s Cancel button. We will create a data model in the next section.

.

Use Entity Framework with existing database objects

In this section, we will create an EF data model.

We will choose to “Generate the model from a database”, and use our own personal database.

Before we continue, you must have run the “Copy Northwind Tables” script, which copies a selection of Northwind sample database tables into your own personal warp-hosted database.

In a later document, you will learn to create a new model, by visually designing the entities yourself.

.

Right-click your App_Code folder, and Add New Item.

Select the “ADO.NET Entity Data Model” item type.

Enter/specify the suggested name “MyNorthwind.edmx”, and click the Add button.

The Entity Data Model Wizard starts.

At this point, choose to “Generate from database”, and click the Next button.

On the “Choose Your Data Connection” wizard panel, do the following (assuming that this is the first time that you’ve run the wizard to this point):

  1. Click the “New Connection” button.
  2. Fill out the “Connection Properties” dialog, as you learned in a previous post/document.
  3. Returning to the wizard panel, choose “Yes, include the sensitive data in the connection string”.
  4. Finally, the most important step, enter this suggested name in the text box that’s below the “Save entity…” prompt: MyNorthwindEntities

.

Then, click the wizard’s Next button.

The wizard now asks you to “Choose Your Database Objects”. Complete the wizard panel as follows:

  1. Click to select the “Tables” item; this will select all the copied Northwind tables.
  2. Leave the “Pluralize…” and “Include foreign…” check boxes checked/selected.
  3. Finally, and importantly, enter this suggested name in the text box that’s below the “Model Namespace” prompt: MyNorthwindModel

.

Click the wizard’s Finish button.

At this point, the data model will open in the Entity Framework Data Model Designer. You will also see MyNorthwind.edmx, and MyNorthwind.Designer.cs, in your App_Code folder.

.

What is an EF data model?

An EF data model is a data access layer.

It provides an interface to a persistent data store.

The interface enables us to work with entity objects – one at a time, or as part of a collection – in an object-oriented way, that matches our software development efforts.

One of the most notable effects of using a data model is that we work more with first-class objects (people, cars, products, invoices), without caring about the back-end storage scheme.

.

Guidance on naming when creating an Entity Framework data model

Pay attention to the names that you use, when creating an Entity Framework data model.

.

Add New Item dialog

To create an Entity Framework data model, you choose the “ADO.NET Entity Data Model” template. Here, you are creating an “.edmx” container for all the objects that together, form an Entity Framework data model. Then, you enter a name for it.

The name’s format doesn’t matter very much. Its name is used only by you, as a source code file/object, in your project/solution.

.

Choose Your Data Connection dialog

The connection string textbook at the bottom has two purposes:

  • A connection string name
  • A class name for the Entity Framework data model

.

The latter purpose – a class name for the entity container – is very important. We use that name in our code, whenever we need to use the entity data model class.

Therefore, for the first few Entity Framework data models that you create, you should choose a name that ends with “…Entities”. The first part of the name can be descriptive, or can help identify the data you’ll be working with. A suggestion – keep the first part of the name fairly short.

.

Choose Your Database Objects dialog

After you select the database objects (i.e. tables, views, stored procedures), you must enter a model namespace name. Its name is fairly important, as we will use the name in our code.

For the first few Entity Framework data models that you create, you should choose a name that ends with “…Model”.

Further, you should consider reusing the first part of the name from the previous step (Choose Your Data Connection dialog).

.

Summary of naming suggestions

New ADO.NET Entity Data Model item – <Whatever>.edmx

Connection string and entity container name – <Descriptive>Entities

Model namespace name – <Descriptive>Model

.


.

.

.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: