Entity framework will always try to connect to the local SQL Server Express database (.\SQLEXPRESS). Starting with EF 5, Ef will use LocalDb if it doesn’t detect SQL Express running. SQL Express will always get precedence if it is installed, even if you are using Visual Studio 2012.

The first step I would recommend is trying to determine what connection string the framework is using, because the exception doesn’t tell you the connection string, and the connection string can be controlled by a variety of conventions, configurations, and code.

To find out the connection string, I’d add some logging to a default constructor in my DbContext derived class.

public class GuestbookContext : DbContext
{
    public DepartmentDb()
    {
        Debug.Write(Database.Connection.ConnectionString);
    }

    public DbSet<Person> People { get; set; }
}

Other than the default options, you might want to connect to a SQL Server Compact 4.0 Local Database.

For this purpose, you must tell EF to use SQL CE as the default Connection factory.

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="System.Data.SqlServerCe.4.0" />
  </parameters>
</defaultConnectionFactory>

This tells EF that SqlCeConnectionFactory is the default and it will look for the database with the same name as the DbContext derived class. E.g If the class name is GuestbookContext, I would look for database Guestbook in the default data folder.

You can also give the connection string name or database name on the DbContext constructor.

There are posts that say you need to install EF SQL Compact NuGet package, however, I did not need it to be installed to get it working. You can install it if the above configurations do not work for you.

Related Links

Advertisements

Share your thoughts

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s