ADO.NET Entity Framework and SQL Server CE 3.5

Introduction

The ADO.NET Entity Framework was introduced as part of Microsoft Visual Studio 2008 SP1 and is a new piece of the .NET framework that comes with the .NET 3.5 SP1 update. The Entity Framework is a set of technologies that extends the already robust functionality found in ADO.NET. For those not familiar with its purpose, the ADO.NET Entity Framework creates a layer of abstraction between the application and the data. The power in creating the layer of abstraction is that a developer can develop against the model, using and consuming objects as any other object in code, while behind the scenes the ADO.NET Entity Framework handles the interactions with the data source. For applications using a relational database, this means that the developer does not need to worry about column names, tables and relationships.

SQL Server Compact Edition (CE) is a small, light weight and free embedded database engine that is purposed well for desktop and Windows mobile device applications. SQL Server CE is the smallest offering of the SQL Server family with full blown SQL Server at one end, SQL Server Express in the middle and SQL Server CE on the small end. While many differences exist, the major differences between SQL Server CE and other versions SQL Server are that CE does not have an installed database engine service (footprint is less than 3MB of DLLs), stored procedures are not supported and simultaneous use by more than one user is limited. Additionally, SQL Server CE database files have the extension of “.sdf” instead of the “.mdf” extension found on SQL Server and SQL Server Express database files.


Whether new to the Compact Edition of SQL Server or to .NET Framework and the ADO.NET Entity Framework, this article will provide the necessary information to productively use both together. Before getting started there are several prerequisites you will need to have installed before being able to develop with both the ADO.NET Entity Framework and SQL Server CE 3.5.

As mentioned previously, the ADO.NET Entity Framework was made available as part of Microsoft Visual Studio 2008 SP1 update. Updating VS 2008 to SP1 will enable the use of the ADO.NET Entity Framework as well as numerous other valuable enhancements. SQL Server CE can be downloaded (if not already installed as part of your Visual Studio SP1 installation) from the Microsoft Download Center.

The following walkthrough example will be an application used to manage suppliers and the products that they supply. The database used in the walkthrough will be the Northwind SQL Server CE database. The database file “Northwind.sdf” can typically be found in the following directory depending on how you installed SQL Server CE 3.5. “C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Samples”

Walkthrough Example Setup

Open up Visual Studio 2008 SP1 and create a new Windows Forms project. On the form, add the following controls:


  • DataGridView – dgvSuppliers

  • DataGridView – dgvProducts

  • Button – cmdSave

  • Button – cmdClose

When finished your forms should look similar to the one captured in the screen shot below.



Figure 1

Next, create and wire-up the events for the Form load and the Close click. Inside the Close click event add the line ‘this.Close();’. Before we can wire up the Form load event to populate the Supplier’s data grid, we must create and configure the ADO.NET Entity Data Model for the Northwind database.

More by Author

Must Read