Change Database during runtime in Entity Framework, without changing the Connection

前端 未结 6 1656
一生所求
一生所求 2020-12-14 01:59

I have a server that hosts 50 databases with identical schemas, and I want to start using Entity Framework in our next version.

I don\'t need a new connection for ea

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 02:32

    It is very simple

    I had

    public WMSEntities() : base("name=WMSEntities") //WMSEntities is conection string name in web.config also the name of EntityFramework
    {
    }
    

    already in autogenerated Model.Context.cs of edmx folder.

    To connect to multiple database in runtime, I created another constructor that takes connection string as parameter like below in same file Model.Context.cs

    public WMSEntities(string connStringName)
                : base("name=" + connStringName)
    {
    }
    

    Now, I added other connection string in Web.Config for example

    
                                                            
提交回复
热议问题