Can the provider connection string of Entity Framework be substituted with a DB connection string already defined in web.config?

会有一股神秘感。 提交于 2019-12-10 21:21:43

问题


I have a db connection string 'ApplicationServices' defined in the connectionString section of web.config and 3 Entity Framework connection strings which have the provider connection string attribute with the same connection string as the one in 'ApplicationServices'.

Is there a way to reference connectionString in 'ApplicationServices' for the provider connection string attribute of the EF connection string in the web.config, rather than providing the connection string all over again? This will reduce errors and help deploy the application more easily.


回答1:


The generated object context has many different ways to provide it with a connection string. Also because the generated class is a partial you can define your own constructor which sets the connection string.

http://msdn.microsoft.com/en-us/library/bb156503.aspx

Reply to comment:

So read from the config:

public class MyContext : ObjectContext 
{
     public MyContext()
         : base(ConfigurationManager.ConnectionStrings["MyConnectionString"])
     { }
}


来源:https://stackoverflow.com/questions/4876880/can-the-provider-connection-string-of-entity-framework-be-substituted-with-a-db

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