Connecting to a SQL Compact database using Entity Framwork in ASP.NET

Michael Roma on Apr 3, 2013

The following shows how to setup a proper connection string and DbContext in Entity Framework in order to connect to a SQL Compact database in ASP.NET.

Here is an example configuration string in your Web.Config: Here is the code for a base DbContext that uses the above connection string:

public class BaseRepository : DbContext, IDisposable
{
    // constructor, connect to database
    public BaseRepository()
        : base(new SqlCeConnection(“myDatabase”),
                 contextOwnsConnection: true) { }        
    
    // dispose
    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
    }   
}