Entity Framework

When we have a database linked to our ASP.NET website, if we want greater control over the way we create, retrieve, display and update and display data within our website, we can choose to view the entries in our database as objects within the C# code that runs 'behind' the website.

The componant that allows us to do this is known as an Object-relational mapping tool (ORM), of which there are a number available for ASP.NET.

Two are included with the default installation of Visual Studio - Linq-to-SQL and Entity Framework. We will be working with Entity Framework because it allows us to work with both SQL Compact Edition (SQL CE) and SQL Server databases (Linq-to-SQL will not work with SQL CE).

Preparation

This tutorial is based on a very simple blogging application, and requires you to create a new empty website, and add a database connect to a database, and create one table named BlogEntries containing the following fields.

Name DataType Notes
Id int Primary Key, Set as identity
Title nvarchar(100)
Body nvarchar(MAX)
Date datetime Do not allow nulls

Creating the Entity Framework Model

Final step from the EF model creation wizard