I\'m trying to use EF Code First with my WPF application , the idea is to create a SqlCe Db in AppData/MyApp (if there isn\'t one) and use it with EF Code First.
At the
I've managed to get it working after a lot of messing around. My app.config has the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Database"
connectionString="Data Source=Database.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0"
invariant="System.Data.SqlServerCe.4.0"
description=".NET Framework Data Provider for Microsoft SQL Server Compact"
type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
</DbProviderFactories>
</system.data>
</configuration>
Having the DbProviderFactory in there will help when it comes to deployment too. It will allow users to use SQLCE 4 without running the installer for it (providing you supply the native binares as well, some info here: http://erikej.blogspot.com/2011/02/using-sql-server-compact-40-with.html).
I don't think the tooling is ready for SQLCE for WPF projects. It stopped me in my tracks too.
Here is a possible clue. If you were to install EFCodeFirst.SqlServerCompact using NuGet Package manager, and look in: Visual Studio 2010\Projects\MyProject\packages\EFCodeFirst.SqlServerCompact.0.8.8482\Content, it does things for your web app like:
public static class AppStart_SQLCEEntityFramework {
public static void Start() {
DbDatabase.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
// Sets the default database initialization code for working with Sql Server Compact databases
// Uncomment this line and replace CONTEXT_NAME with the name of your DbContext if you are
// using your DbContext to create and manage your database
//DbDatabase.SetInitializer(new CreateCeDatabaseIfNotExists<CONTEXT_NAME>());
}
}
This probably isn't going to help in the short run, but I think it would be the best solution in the long term. I haven't been able to find where anyone else has documented how to do this so I guess I'll have to figure it out as I go unless someone with more intimate knowledge of using SqlCE4, CodeFirst, and creating NuGet packages beats me to it. What we need is a NuGet package, either a new package or make the existing one smart enough to handle it, that can add SqlCE4 and CodeFirst to a WPF and/or WinForms project.
The existing package seems geared toward web projects since it depends on WebActivator. From what I can tell, installing WebActivator on a non-web project really isn't appropriate. I don't know if it's possible for a NuGet package to detect the project type and execute different installation logic based on that information. It would be better all around for whoever is responsible for maintaining the EFCodeFirst.SqlServerCompact package to either release a non-web version or make the existing one smarter.
Pending that, in my spare time I'm going to see if I can figure out how to do it and create my own. I can't promise how long it'll take me because I'm almost totally in the dark with regards to creating NuGet packages. I've created a couple of really basic ones but have never done any advanced stuff like config transforms and the like.