private void UpdateConnectionStringIfNeed(DbConnection connection, string connectionString, IEnumerable <System.Data.Entity.DbContext> contexts) { if (contexts.Any(this.Config.CanApplyTo)) { if (!DbMasterSlaveCommandInterceptor.ConnectionStringEquals(connection, connectionString)) { DbMasterSlaveCommandInterceptor.UpdateConnectionString(connection, connectionString); } } }
/// <summary> /// 将指定的 EF 实体数据库上下文类型注册到读写分离服务中。这是 EF 读写分离服务启动的入口点。 /// <para>注意:传入的参数 <paramref name="contextType"/> 所表示的类型必须是 <see cref="System.Data.Entity.DbContext"/> 或者该类型的子类型。</para> /// </summary> /// <param name="contextType"></param> public static void Register(Type contextType) { Check.NotNull(contextType); DbMasterSlaveCommandInterceptor commandInterceptor = new DbMasterSlaveCommandInterceptor(contextType); DbMasterSlaveConnectionInterceptor connectionInterceptor = new DbMasterSlaveConnectionInterceptor(commandInterceptor.Config); DbInterception.Remove(commandInterceptor); DbInterception.Remove(connectionInterceptor); DbInterception.Add(commandInterceptor); DbInterception.Add(connectionInterceptor); }