CraigF
CraigF

Reputation: 139

Entity Framework 4.2 code first in a class library ignoring my connection string

I'm building a class library for an application (exitgames photon) and it doesn't have a web.config or an app.config.
Therefore I am setting the connection string against the context like this:

db.Database.Connection.ConnectionString = "Data Source=machinename\\SqlExpress;Initial Catalog=AwesomeDB;Integrated Security=True";

The context (db) then ignores the connection string and uses the default one as if I didn't specify it (based on the namespace and context name etc).

Anyone know how to specify the connection string inline thus removing the requirement for an app.config?

Upvotes: 0

Views: 664

Answers (3)

Eranga
Eranga

Reputation: 32447

I wouldn't recommend hard coding the connection string. You can pass the connection string in the default constructor.

public class MyContext : DbContext
{
    public MyContext()
    : base("Data Source=machinename\\SqlExpress;Initial Catalog=AwesomeDB;Integrated Security=True")
    {
    }
}

Upvotes: 1

ArunJohney
ArunJohney

Reputation: 91

You can pass the connection string as a parameter to the constructor of the context.

Upvotes: 0

J.W.
J.W.

Reputation: 18181

If it's a class library, then it will use the connection string specified in web.config (web site) or app.config (application) which will use this library.

Upvotes: 2

Related Questions