Microsoft Entity Framework Intro (Part 1 of 3)

Entity Framework - Introduction (Part 1 of 3)

(Part 2, Part 3)

The following tutorial will explain some basic concepts using the Microsoft Entity Framework, I'll attempt to explain this with a few examples to help demonstrate some of the capabilities.

Recomendations for this tutorial, other versions of these products may work however these are the versions used in this tutorial.

1. Microsoft Visual Studio 2012
2. SQL Server 2008R2
3. AdventureWorks database (2008R2 version) downloaded and installed from http://msftdbprodsamples.codeplex.com/downloads/get/478216


The Microsoft Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. This creates, in effect, a "virtual object database" that can be used from within a project to access database objects using C# code instead of having to write a whole lot of SQL queries. In this post we'll add a sample database to our SQL server, create a new Entity Framework model from our existing sample database and try and get an understanding of the model.

Part 1 - Database First

Adding the AdventureWorks Database - If you do not have the demo database attached to your SQL Server you can download the database from the link above. Once downloaded attach the database to SQL Server.

Step 1 - Copy the database files into the correct file locations for your SQL Server. The .mdf file should be copied into the data folder where all the other SQL Databases exist and the .ldf file should be copied into the the Log folder where all the other Log files exist.

Step 2 -From the SQL Server Management Console right click on databases and select Attach.

Step 3 - Select Add and browse to the correct .mdf file located where you copied it on the SQL Server

Step 4 - Select the ellipsis next to the log file and ensure the log file path is correct

Step 5 - Ensure the AdventureWorks database is correctly attached to your SQL Server

Creating the Visual Studio Project - Next we'll create a Visual Studio Project to demonstrate some of the Entity Framework capabilities.

Note: If you are using a version of Visual Studio other than 2012, you may not have the Entit

In this example I'll create a new class library project, I'll be able to add or reference this project in any new solution that I create in order to leverage the Entity Framework.

Create a new class library and name it EFModel

Right click on the project name and select add, add a new ADO.NET Entity Data Model

Create an Entity Data Model From a database

Create a new connection to your database

rename the connection settings AWEntities

Select Tables and Views to be populated from the database into our new Entity Model and select Finish

We can now see a new Entity Framework model has been created called EFModel, this will be instantiated in our project when we want to leverage the Entity Framework.

The designer view of the Entity Model will be displayed, we can view all of the relationships or associations in the designer. Notice the relationship between Customer and SalesOrderHeader is a one to many relationship (depicted by the 1 on customer and the asterisks on SalesOrderHeader)

In our project an App.Config file has been created, within this file an Entity connection string can be found.

If we right click on the white space in the designer view of our Entity Model and select Mapping Details, then select one of the entities in the diagram, the database column to class property mapping can be viewed or modified.

The mapping details shows the relationships between the database columns and the model properties, these are also customize-able.


This wraps up part one of the introduction to the Entity Framework, we covered getting a sample database installed on our SQL Server 2008R2 Database Server, Creating a new Class Library project, adding an Entity Framework model created from our existing AdventureWorks database and briefly looking at our completed Model. In part two we'll cover creating a SQL database from an existing model.

(Part 2, Part 3)

No comments:

Post a Comment