I am developing an app with Entity Framework 4 and SQL Compact 4, using a Model First approach. I have created my EDM, and now I want to generate a SQL Compact 4.0 database
You can use IDatabaseInitializer and create the database in code using
if (File.Exists("Test.sdf"))
File.Delete("Test.sdf");
string connStr = "Data Source = Test.sdf; Password = <password>";
SqlCeEngine engine = new SqlCeEngine(connStr);
engine.CreateDatabase();
engine.Dispose();
SqlCeConnection conn = null;
try {
conn = new SqlCeConnection(connStr);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "CREATE TABLE myTable (col1 int, col2 ntext)";
cmd.ExecuteNonQuery();
catch {
finally {
conn.Close();
http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceengine(v=vs.80).aspx
Probably need the SQL Server Compact 4 tools installed as well, if it did not install directly when Visual Studio 2010 SP1 was installed, you can install it. This is what worked for me when I had the problem.
To answer the comments below and address them according to: http://blogs.msdn.com/b/sqlservercompact/archive/2011/01/12/microsoft-sql-server-compact-4-0-is-available-for-download.aspx
These wizards to not work.
Designers in the VB or C# Windows projects in Visual Studio 2010 SP1 Beta: The following wizards do not work with Compact 4.0 in the Windows project system. Developers can manually add reference to the ADO.NET provider for Compact 4.0 (System.Data.SqlServerCe) to develop programs for Compact 4.0 in the Windows projects:
Here are the accepted work arounds for these issues. http://erikej.blogspot.com/2010/11/using-entity-framework-with-sql-server.html
As I understand it current version of VS 2010 doesn't have support for SQL CE 4.0. It should be included in VS 2010 SP1 (currently in Beta). Check this blog post which also describes using EF Model with SQL CE 4.0 in SP1.
Edit:
I found this workaround.
I can run Generate Database Wizard with out any issue from my Chinook.Data project, make sure you have a proper connection string in your app.config. http://erikej.blogspot.com/2010/11/using-entity-framework-with-sql-server.html
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="ChinookEntities" connectionString="metadata=res://*/ChinookModel.csdl|res://*/ChinookModel.ssdl|res://*/ChinookModel.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=C:\projects\Chinook\Chinook40.sdf"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Yet another, use WebMatrix database tool.
Microsoft WebMatrix