Using Sql Compact 4 in WPF Application project

Earlier today, i’m having trouble generating database model using Entity Framework and SQL CE 4 in WPF Application project.

after googling around, it seems there’s quite a few people that’s having the same problem, it’s a different story if you’re using it in a web application project, there’s a selection for SQL CE 4 when you’re trying to generate database like shown below.

here’s the workaround on getting the EF play nice on WPF app project:

    1. create a new WPF app project
    2. right click the project and select add new item
    3. choose sql compact 4
    4. right click the project and select add new item
    5. choose ADO.NET Entity Data Model
    6. on the EF Visual Designer, right click and choose Update Model from Database


notice that it doesn’t have SQL CE 4 as a data source

here’s what we’ll do

  1. right click the project and select add new item
  2. choose Application Configuration File
  3. open the configuration file, and add in this line below inside the configuration tag
<connectionStrings>
        <add name="{EdmxContainerName}" connectionString="metadata=res://*/{EdmxFileName}.csdl|res://*/{EdmxFileName}.ssdl|res://*/{EdmxFileName}.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data Source=EzWorklog.sdf&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>

notice the {EdmxFileName} and {EdmxContainerName} that i put on the connection string, fill it with the appropriate value.
now go back to the EF visual designer and choose Update model from database
now it will know the database that we specified in our application configuration file

Happy coding

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s