EF6 动态设置DbContext的连接字符串

    public class StudentContext : DbContext
    {
        public StudentContext() : base(connstr)
        { }
        public StudentContext(string studentdb) : base(studentdb)
        { }

        public DbSet<Student> Stusdents { get; set; }

        public static string connstr = "studentdb";
    }

如上代码,可以通过静态变量设置连接字符串后再调用无参数构造函数或者使用有参数的构造函数。本来想去掉无参数的构造函数,但是发现Magration时,比如Add-Migration操作会调用无参的构造函数比较数据库生成升级文件,所以必须提供无参数的构造函数

原文地址:https://www.cnblogs.com/lidaying5/p/13096607.html