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:

Imports System.Data.EntityClient
Imports SQLCompactEDM.NorthwindModel

Module Module1
    Sub Main()
        Console.WriteLine("SQL Server Compact and Northwind Entity Framework Sample:")
            Using connection As EntityConnection = _
                New EntityConnection("Name = NorthwindEntities")
                ' Open the connection.

                ' A simple query that demonstrates how to use ESQL for entities.
                Console.WriteLine(vbNewLine & "ESQL 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(vbNewLine & "LINQ To Entity Query:")

                ' A simple query that demonstrates how to use ObjectContext
                ' on data objects with an existing connection.
                Console.WriteLine(vbNewLine & "Object Query:")

                ' Close the connection.
            End Using
        Catch ex As Exception
        End Try
    End Sub

    ' A simple query that demonstrates how to use ESQL for entities.
    Private Sub ESQL_Query(ByVal entityConnection As EntityConnection)
        Dim entityCommand As EntityCommand = entityConnection.CreateCommand
        entityCommand.CommandText = _
            "Select Cust.Customer_Id as Id from NorthwindEntities.Customers as Cust order by Cust.Customer_Id"

        Dim entityDataReader As EntityDataReader = _

        Do While entityDataReader.Read
            Dim i As Integer
            For i = 0 To entityDataReader.FieldCount - 1
                Console.Write((entityDataReader.Item(i).ToString & vbTab))
            Next i
    End Sub

    ' 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 Sub LINQ_To_Entity_Query()
        Using nwEntities As NorthwindEntities = New NorthwindEntities
            Dim customers = From c In nwEntities.Customers Select c.Company_Name

            For Each c In customers
        End Using
    End Sub

    ' 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 Sub ObjectQuery(ByVal entityConnection As EntityConnection)
        Using nwEntities As NorthwindEntities = New NorthwindEntities(entityConnection)
            Dim c As Customers
            For Each c In nwEntities.Customers
        End Using
    End Sub
End Module