在以前的版本中咱們可用構造函數實現,其實如今的版本也同樣,以前來構造鏈接字符串,如今類似,構造DbContextOptions<T>函數
代碼以下:ui
public SContext(MasterSlave masterSlave) : base(GetOptions(masterSlave)) { // TODO: #639 //ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; } /// <summary> /// 主從關係 /// </summary> /// <param name="writeRead"></param> /// <returns></returns> private static DbContextOptions<SContext> GetOptions(MasterSlave masterSlave) { var optionsBuilder = new DbContextOptionsBuilder<SContext>(); if (masterSlave == MasterSlave.Slave) { optionsBuilder.UseSqlServer(Config.DbConfig.Read); } else { optionsBuilder.UseSqlServer(Config.DbConfig.Write); } return optionsBuilder.Options; }