Tech Junkie Blog - Real World Tutorials, Happy Coding!: Entity Framework Part 1: Generate an Entity Framework for the Northwind Database Tables

Sunday, June 19, 2016

Entity Framework Part 1: Generate an Entity Framework for the Northwind Database Tables

In my previous post we've created the Northwind products page using the SqlDataSource data control, even though there's nothing wrong with the solution.  SqlDataSource is a an old technology and is not meant to be an enterprise solution.  SqlDataSource is the stone age of .NET data access technology.  The future of .NET is the Entity Framework.

The goal of the Entity Framework is to create a conceptual model of your data store so that you can work with tables and rows as objects, or entities.  In essence the developer does not know or care what the data store is, he does not have to be a dba to work with the data.

To demonstrate the Entity Framework let's create the Northwind products page using the Entity Data Model of the Northwind database.

Here are the steps:

1. Open the Northwind project you created with the SqlDataSource



2. Righ-click on the Northwind project in "Solution Explorer", then select "Add", then "New Item"

Solution Explorer

3. Select "Data" under "Visual C#" in the "Add New Item" dialog box, then select "ADO.NET Entity Data Model"




4. In the "Choose Model Contents" dialog box, choose "Generate from database", then click "Next"

Generate from database


 5. On the "Choose Your Data Connection" dialog box, click on the "New Connection" button, then type in the name of your database server in the "Server name:" field and select the "Northwind" database in the "Connection Properties" dialog box.

Connection Properties

 6. Select the "Northwind" database connection, and save the Entity connection as "NorthwindEntities", then click "Next"

Entity Data Model Wizard

 7. Choose all of the database tables in the "Choose Your Database Objects and Settings" dialog box, leave the "Model Namespace", then click "Finish".  Entity Framework for the "Northwind" database model will be generated for you in Visual Studio.

Northwind database model


 8. In "Solution Explorer" there is also a "NorthwindModel.edmx" file that contains the Entity and data store mappings in raw XML.

9. Now to query the Northwind model you need to use LINQ, create a new Web Form and call it Products.aspx

10. Add a GridView to the design surface


11. Open the code-behind page Products.aspx.cs then in the Page_Load method type in the following
        

       protected void Page_Load(object sender, EventArgs e)
        {
            NorthwindEntities nEntitites = new NorthwindEntities();

            var products = nEntitites.Products;

            GridView1.DataSource = products;
            GridView1.DataBind();

        }

        
In the code above you instantiate a new NorthwindEntities model, then assign the Products collection into the variable products.  The Products entity contains a collection of the Product object, it's like querying the Products table but you are working directly with the object created by the Entity Framework instead querying the database directly.

12.  To run the application press F5, you will see that the GridView control has been populated with data

GridView

Source Code: Northwind.zip, click "File" -> "Download" to download the source code in .zip format.

1 comment:

Search This Blog