SQL Server 2008 R2

This topic provides step-by-step instructions on how to create Entity Framework applications that use the SQL Server Compact database as a data source.

To create a new Entity Framework application

  1. In Visual Studio, on the File menu, point to New, and then select Project.

  2. In the Project Types list of the New Project dialog box, expand the programming language you will use and select Visual C# or Visual Basic.

  3. In the Templates list, select Console Application.

  4. Provide a name (such as SQLCompactEDMProject) and location for your project, and then click OK.

  5. To generate the Entity Data Model for the Northwind.sdf, copy the Northwind.sdf from the folder %ProgramFiles%\Microsoft SQL Server Compact Edition\v3.5\Samples to the folder that your project exists.

  6. On the Project menu, click Add new item.

  7. In the Templates pane, select ADO.NET Entity Data Model.

  8. Type Northwind.edmx for the model name and then click Add.

  9. The first page of the Entity Data Model Wizard is displayed.

  10. In the Choose Model Contents dialog box, select Generate from database. Then click Next.

  11. Click the New Connection button.

  12. In the Connection Properties dialog box, click the Change button in the Data Source. Select Microsoft SQL Server Compact 3.5, browse to the Northwind.sdf and then click OK.

    The Choose Your Data Connection dialog box is updated with your database connection settings.

  13. Ensure that the Save entity connection settings in App.Config as: checkbox is checked and the value is set to NorthwindEntities. Then click Next.

  14. In the Choose Your Database Objects dialog box, clear all objects, expand Tables, and select the Customers as a table object.

  15. Type NorthwindModel for the Model Namespace.

  16. Click Finish to complete the wizard.

  17. The wizard does the following:

    1. Adds references to the System.Data.Entity.dll, System.Runtime.Serialization.dll, and System.Security.dll assemblies.

    2. Generates the Northwind.edmx file that defines the EDM.

    3. Creates a source code file that contains the classes that were generated based on the EDM. You can view the source code file by expanding the .edmx file in Solution Explorer.

    4. Creates an App.Config file.

  18. Double-click the project's application configuration file (app.config) and make sure that provider=System.Data.SqlServerCe.3.5 in the connection string.

  19. In the code page for your application, add the following using statements:


    using NorthwindModel;

    Visual Basic:

    Imports SQLCompactEDM.NorthwindModel

    Note that the name of the model corresponds to the value of the namespace specified in the Northwind.edmx file.

  20. Copy the following code example to your code file. Then, compile and run.

The System.Data.Entity.dll assembly is a part of the SP1 release of .NET Framework version 3.5. Managed reference topics for the System.Data.Entity assembly are available in the Entity Framework documentation.

The following code example provides three simple Entity Framework queries against the SQL Server Compact Northwind.sdf database:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using System.Data;
using NorthwindModel;

namespace SQLCompactEDMProject
  class SSC_EntityFrameworkExample
    static void Main(string[] args)
        "SQL Server Compact and Northwind Entity Framework Sample:");
        // Establish a connection to the underlying data provider by 
        // using the connection string specified in the config file.
        using (EntityConnection connection = 
             new EntityConnection("Name = NorthwindEntities"))
          // Open the connection.

          // A simple query that demonstrates 
          // how to use Entity SQL for entities.
          Console.WriteLine("\nEntity SQL Query:");

          // A simple query that demonstrates 
          // how to use LINQ to Entities.
          // A new connection is established by using the connection 
          // string in the App.Config file.
          Console.WriteLine("\nLINQ To Entity Query:");

          // A simple query that demonstrates how to use ObjectContext
          // on data objects with an existing connection.
          Console.WriteLine("\nObject Query:");

          // Close the connection.
     catch (Exception ex)

  // A simple query that demonstrates how to use 
  // Entity SQL query for entities.
  private static void ESQL_Query(EntityConnection entityConnection)
    EntityCommand entityCommand = entityConnection.CreateCommand();
    entityCommand.CommandText =
      @"Select Cust.Customer_Id as Id from NorthwindEntities.Customers as Cust order by Cust.Customer_Id";

     EntityDataReader entityDataReader =

     while (entityDataReader.Read())
       for (int i = 0; i < entityDataReader.FieldCount; i++)
         Console.Write(entityDataReader[i].ToString() + "\t");

  // A simple LINQ query on the Customers entity set in the Northwind 
  // Context.
  // The code example creates a new Northwind Context based on the 
  // settings provided in the App.Config File.
  private static void LINQ_To_Entity_Query()
    using (NorthwindEntities nwEntities = new NorthwindEntities())
      IQueryable<string> customers =
         from c in nwEntities.Customers select c.Company_Name;

      foreach (String c in customers)

  // The ObjectContext provides access to the collections of data used 
  // by applications. 
  // The following code shows how to open a connection to the 
  // ObjectContext named NorthwindEntities. 
  // With the application configuration file in scope the connection 
  // can be opened with one line of code: 
  // NorthwindEntities nwEntities = new NorthwindEntities(entityConnection).
  private static void ObjectQuery(EntityConnection entityConnection)
     using (NorthwindEntities nwEntities = new 
        foreach (Customers c in nwEntities.Customers)