12

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?

tafkab76
  • 465
  • 1
  • 6
  • 18
  • Did you change the connection string? – Rand Random Oct 31 '13 at 14:44
  • I just came here to write that it works now, after I manually added a connection string, so you are totally right. Is that the correct way to do it, manually adding a connection string to the app.config? (There was no kind of connection string before). – tafkab76 Oct 31 '13 at 14:57
  • Yes, because without its not using a real SqlServer but the inbuild SqlLite edition of MSSqlServer. – Rand Random Oct 31 '13 at 15:02
  • Pls write your self an answer and accept it, so that this question doenst stay open all eternity. – Rand Random Oct 31 '13 at 15:24
  • I tried that right away, but due to my poor reputation I´ll have to wait at least eight more hours to answer my own question. – tafkab76 Oct 31 '13 at 15:27
  • Ah okay, never tried so didnt know. :) – Rand Random Oct 31 '13 at 15:31

1 Answers1

13

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>
tafkab76
  • 465
  • 1
  • 6
  • 18