开发者

Specify ConnectionString for MVC DataContext globally

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜