Entity Framework 6 with SQL Server Compact 4.0

entity-framework sql-server-ce

Question

I'm (still) trying to follow an online video tutorial on how to use the Entity Framework with the code-first approach. I am as far as my EDM validates fine and it's time to actually build the database. The instructor uses a SQL Server and obviously the database is created automatically when the first query command (ToList()) against an DbContext-object.

As I am planning to use a SQL Server Compact file-based database in a future application, I tried to reproduce the tutorial's example with SQL Server Compact 4.0. I installed the NuGet packages "EntityFramework.SqlServerCompact" and "Micrososft SQL Server Compact Edition". Obviously this leads to a new provider entry in the app.config:

<providers>
  <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>

But when I run the first query command, I get the error message

Migrations is enabled for context 'DataModelContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.

When I run Update-Database I get the message:

No pending explicit migrations.
Applying automatic migration: 201310311356014_AutomaticMigration.
Running Seed method.

and no database is created, at least the program still quits with above error message.

Doesn't EF create a SQL Server CE database automatically? If so, how do I connect a manually created DB with my app?

1
12
2/25/2014 10:04:51 AM

Accepted Answer

The problem could be solved by manually adding a Connection string to the app.config-file, at the end of the <configuration>-tag:

  <connectionStrings>
    <add name="DataModelContext"
         providerName="System.Data.SqlServerCe.4.0"
         connectionString="Data Source=C:\Users\sblxxx\Documents\Visual Studio 2012\Projects\Pluralsight\EF5\CodeFirst\db\cfdb.sdf"/>
  </connectionStrings>
13
11/1/2013 8:01:29 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow