Search code examples
asp.net-mvcentity-frameworkcode-firstlocaldb

Keyword not supported: 'data source'.: EF code-first using ObjectContext and LocalDB


I am getting a "keyword not supported error" when I try to connect to a LocalDB database using ObjectContext.

This is my connection string:

<add name="connStr" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=proj1db;Integrated Security=True" />

and this is the code that tries to create an instance of the ObjectContext:

var connectionString = ConfigurationManager
                .ConnectionStrings["connStr"]
                .ConnectionString;
ObjectContext _context = new ObjectContext(connectionString);

The last line throws System.ArgumentException: Keyword not supported: 'data source'.

I am using Visual Studio 2012 for Web and targeting .NET Framework 4.5. I have LocalDB installed on my machine.

If I use DbContext instead it works:

public class proj1dbContext: DbContext
{
    public proj1dbContext() : base("name=connStr")
    ...

It seems that this is a similar question Help with EF Code first connection string but unfortunately it does not give a definitive answer to why instantiating ObjectContext throws that error.

Any help is appreaciated. Thanks!


Solution

  • ObjectContext takes an EF connection string (with Metadata and Provider Connection String keywords), not a provider-specific connection string.

    You can't use ObjectContext with Code-First; ObjectContext requires the metadata XML files.