开发者

Specify ConnectionString for MVC DataContext globally

开发者 https://www.devze.com 2023-02-23 13:23 出处:网络
I have an MVC3 Application that allows a user to choose the database they want to use on initial login.

I have an MVC3 Application that allows a user to choose the database they want to use on initial login. The various connection strings are available in the web.config I would like every DataContext to use the chosen connection.

I am aware that I can supply a parameter to one instance of a named DataContext l开发者_JAVA百科ike this:

MyDataContext db = new MyDataContext(ConnectionString);

or I can override the OnCreated event for all instances of a named DataContext

public partial class MyDataContext
{
    partial void OnCreated()
    {
        Connections connections = new Connections();
        this.Connection.ConnectionString = connections.GetCurrentConnectionString();
    }
}

how about all instances of all DataContexts throughout the application?

0

精彩评论

暂无评论...
验证码 换一张
取 消