Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 6 with SQL Server Compact 4.0

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?

like image 900
tafkab76 Avatar asked Oct 31 '13 14:10

tafkab76


1 Answers

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>
like image 160
tafkab76 Avatar answered Sep 30 '22 06:09

tafkab76