Entity Framework : Change connection string at runtime

后端 未结 6 1721
半阙折子戏
半阙折子戏 2021-02-04 03:06

Assuming there is an ASP.NET MVC application that uses Entity Framework 6 with code-first approach and StructureMap as IoC.
Also It uses Unit Of Work pattern. Here are the c

6条回答
  •  时光取名叫无心
    2021-02-04 03:36

     public partial class YourDBContextClass
     {
        // Add a constructor to allow the connection string name to be changed
     public YourDBContextClass(string connectionStringNameInConfig)
            : base("name=" + connectionStringNameInConfig)
        {
        }
    }
    

    Add multiple connection strings to your web or app.config file.

    in your program code:

    YourDBContextClass dbcontext = new YourDBContextClass("alternateconnectionstringname");
    

提交回复
热议问题