Search code examples
asp.net-mvcconnection-stringsdf

Switching from SQL Server Express to SDF database


I've created an mvc project with following connection string (which is a default):

<add name="TestDb" 
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" 
     providerName="System.Data.SqlClient" />

I would like to switch from SQL Server Express to one of the file databases, so that I can store my db in same repository. How can I do this (steps/connection string) ?

I'm using MVC code first approach. Thank you.


Solution

    1. Install the the EntityFramework.SQLServerCompact NuGet package.
    2. Change your connection string to this:

      <add name="TestDb" providerName="System.Data.SqlServerCe.4.0" connectionString= "Data Source=|DataDirectory|\aspnetdb.sdf" />

    With Code First, you typically use the context class name as your connection string name.