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

亡梦爱人 提交于 2019-12-12 03:17:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!