问题
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
?
回答1:
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")
{
}
}
回答2:
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.
回答3:
You can pass the connection string as a parameter to the constructor of the context.
来源:https://stackoverflow.com/questions/9089605/entity-framework-4-2-code-first-in-a-class-library-ignoring-my-connection-string